{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# TensorFlow基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import tensorflow.keras as keras\n",
    "import tensorflow.keras.layers as layers\n",
    "\n",
    "physical_devices = tf.config.experimental.list_physical_devices('GPU')\n",
    "assert len(physical_devices) > 0, \"Not enough GPU hardware devices available\"\n",
    "tf.config.experimental.set_memory_growth(physical_devices[0], True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据类型\n",
    "\n",
    "### 数值类型\n",
    "\n",
    "标量在 TensorFlow 是如何创建的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(float, tensorflow.python.framework.ops.EagerTensor, True)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# python 语言方式创建标量\n",
    "a = 1.2 \n",
    "# TF 方式创建标量\n",
    "aa = tf.constant(1.2)\n",
    "\n",
    "type(a), type(aa), tf.is_tensor(aa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果要使用 TensorFlow 提供的功能函数， 须通过 TensorFlow 规定的方式去创建张量，而不能使用 Python 语言的标准变量创建方式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=1, shape=(3,), dtype=float32, numpy=array([1. , 2. , 3.3], dtype=float32)>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.constant([1,2.,3.3])\n",
    "# 打印 TF 张量的相关信息                \n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1. , 2. , 3.3], dtype=float32)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将 TF 张量的数据导出为 numpy 数组格式\n",
    "x.numpy() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与标量不同，向量的定义须通过 List 容器传给 tf.constant()函数。\n",
    "\n",
    "创建一个元素的向量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: id=2, shape=(1,), dtype=float32, numpy=array([1.2], dtype=float32)>,\n",
       " TensorShape([1]))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个元素的向量\n",
    "a = tf.constant([1.2]) \n",
    "a, a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建 3 个元素的向量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: id=3, shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>,\n",
       " TensorShape([3]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " # 创建 3 个元素的向量\n",
    "a = tf.constant([1,2, 3.])\n",
    "a, a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: id=4, shape=(2, 2), dtype=int32, numpy=\n",
       " array([[1, 2],\n",
       "        [3, 4]])>, TensorShape([2, 2]))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 2 行 2 列的矩阵\n",
    "a = tf.constant([[1,2],[3,4]]) \n",
    "a, a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "三维张量可以定义为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=5, shape=(2, 2, 2), dtype=int32, numpy=\n",
       "array([[[1, 2],\n",
       "        [3, 4]],\n",
       "\n",
       "       [[5, 6],\n",
       "        [7, 8]]])>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 3 维张量\n",
    "tf.constant([[[1,2],[3,4]],[[5,6],[7,8]]]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过传入字符串对象即可创建字符串类型的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=6, shape=(), dtype=string, numpy=b'Hello, Deep Learning.'>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建字符串\n",
    "a = tf.constant('Hello, Deep Learning.') \n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串类型\n",
    "\n",
    "通过传入字符串对象即可创建字符串类型的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=7, shape=(), dtype=string, numpy=b'Hello, Deep Learning.'>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建字符串\n",
    "a = tf.constant('Hello, Deep Learning.') \n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在 tf.strings 模块中，提供了常见的字符串类型的工具函数，如小写化 lower()、 拼接\n",
    "join()、 长度 length()、 切分 split()等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=8, shape=(), dtype=string, numpy=b'hello, deep learning.'>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 小写化字符串\n",
    "tf.strings.lower(a) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔类型\n",
    "布尔类型的张量只需要传入 Python 语言的布尔类型数据，转换成 TensorFlow 内部布尔型即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=9, shape=(), dtype=bool, numpy=True>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建布尔类型标量\n",
    "tf.constant(True) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建布尔类型的向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=10, shape=(2,), dtype=bool, numpy=array([ True, False])>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " # 创建布尔类型向量\n",
    "tf.constant([True, False])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "需要注意的是， TensorFlow 的布尔类型和 Python 语言的布尔类型并不等价，不能通用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "tf.Tensor(True, shape=(), dtype=bool)\n"
     ]
    }
   ],
   "source": [
    "# 创建 TF 布尔张量\n",
    "a = tf.constant(True) \n",
    "# TF 布尔类型张量与 python 布尔类型比较\n",
    "print(a is True) \n",
    "# 仅数值比较\n",
    "print(a == True) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数值精度\n",
    "\n",
    "在创建张量时，可以指定张量的保存精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=14, shape=(), dtype=int16, numpy=-13035>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建指定精度的张量\n",
    "tf.constant(123456789, dtype=tf.int16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=15, shape=(), dtype=int32, numpy=123456789>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.constant(123456789, dtype=tf.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于浮点数， 高精度的张量可以表示更精准的数据，例如采用 tf.float32 精度保存π时，实际保存的数据为 3.1415927"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=16, shape=(), dtype=float32, numpy=3.1415927>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 从 numpy 中导入 pi 常量\n",
    "np.pi \n",
    "# 32 位\n",
    "tf.constant(np.pi, dtype=tf.float32) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果采用 tf.float64 精度保存π，则能获得更高的精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=17, shape=(), dtype=float64, numpy=3.141592653589793>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.constant(np.pi, dtype=tf.float64) # 64 位"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取精度\n",
    "\n",
    "通过访问张量的 dtype 成员属性可以判断张量的保存精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "before: <dtype: 'float16'>\n",
      "after : <dtype: 'float32'>\n"
     ]
    }
   ],
   "source": [
    "a = tf.constant(np.pi, dtype=tf.float16)\n",
    "\n",
    "# 读取原有张量的数值精度\n",
    "print('before:',a.dtype) \n",
    "# 如果精度不符合要求，则进行转换\n",
    "if a.dtype != tf.float32: \n",
    "    # tf.cast 函数可以完成精度转换\n",
    "    a = tf.cast(a,tf.float32) \n",
    "# 打印转换后的精度\n",
    "print('after :',a.dtype) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类型转换\n",
    "系统的每个模块使用的数据类型、 数值精度可能各不相同， 对于不符合要求的张量的类型及精度， 需要通过 tf.cast 函数进行转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=21, shape=(), dtype=float64, numpy=3.140625>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 tf.float16 低精度张量\n",
    "a = tf.constant(np.pi, dtype=tf.float16) \n",
    "# 转换为高精度张量\n",
    "tf.cast(a, tf.double) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "进行类型转换时，需要保证转换操作的合法性， 例如将高精度的张量转换为低精度的张量时，可能发生数据溢出隐患："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=23, shape=(), dtype=int16, numpy=-13035>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = tf.constant(123456789, dtype=tf.int32)\n",
    "# 转换为低精度整型\n",
    "tf.cast(a, tf.int16) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "布尔类型与整型之间相互转换也是合法的， 是比较常见的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=25, shape=(2,), dtype=int32, numpy=array([1, 0])>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = tf.constant([True, False])\n",
    "# 布尔类型转整型\n",
    "tf.cast(a, tf.int32) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一般默认 0 表示 False， 1 表示 True，在 TensorFlow 中，将非 0 数字都视为 True，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=27, shape=(4,), dtype=bool, numpy=array([ True, False,  True,  True])>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = tf.constant([-1, 0, 1, 2])\n",
    "# 整型转布尔类型\n",
    "tf.cast(a, tf.bool) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 待优化张量\n",
    "\n",
    "TensorFlow 增加了一种专门的数据类型来支持梯度信息的记录： tf.Variable。 tf.Variable 类型在普通的张量类型基础上添加了 name， trainable 等属性来支持计算图的构建。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Variable:0', True)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 TF 张量\n",
    "a = tf.constant([-1, 0, 1, 2]) \n",
    "# 转换为 Variable 类型\n",
    "aa = tf.Variable(a) \n",
    "# Variable 类型张量的属性\n",
    "aa.name, aa.trainable "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "name 属性用于命名计算图中的变量，这套命名体系是 TensorFlow 内部维护的， 一般不需要用户关注 name 属性；   \n",
    "trainable属性表征当前张量是否需要被优化，创建 Variable 对象时是默认启用优化标志，可以设置trainable=False 来设置张量不需要优化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'Variable:0' shape=(2, 2) dtype=int32, numpy=\n",
       "array([[1, 2],\n",
       "       [3, 4]])>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接创建 Variable 张量\n",
    "tf.Variable([[1,2],[3,4]]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建张量\n",
    "\n",
    "### 从数组、列表对象创建"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过 tf.convert_to_tensor 函数可以创建新 Tensor，并将保存在 Python List 对象或者Numpy Array 对象中的数据导入到新 Tensor 中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=44, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从列表创建张量\n",
    "tf.convert_to_tensor([1,2.]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=45, shape=(2, 2), dtype=float64, numpy=\n",
       "array([[1., 2.],\n",
       "       [3., 4.]])>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从数组中创建张量\n",
    "tf.convert_to_tensor(np.array([[1,2.],[3,4]])) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建全0或全1张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: id=46, shape=(), dtype=float32, numpy=0.0>,\n",
       " <tf.Tensor: id=47, shape=(), dtype=float32, numpy=1.0>)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全 0，全 1 的标量\n",
    "tf.zeros([]),tf.ones([]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: id=50, shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>,\n",
       " <tf.Tensor: id=53, shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全 0，全 1 的向量\n",
    "tf.zeros([1]),tf.ones([1]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建全 0 的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=56, shape=(2, 2), dtype=float32, numpy=\n",
       "array([[0., 0.],\n",
       "       [0., 0.]], dtype=float32)>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全 0 矩阵，指定 shape 为 2 行 2 列\n",
    "tf.zeros([2,2]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建全 1 的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=59, shape=(3, 2), dtype=float32, numpy=\n",
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]], dtype=float32)>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全 1 矩阵，指定 shape 为 3 行 2 列\n",
    "tf.ones([3,2]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过 tf.zeros_like, tf.ones_like 可以方便地新建与某个张量 shape 一致， 且内容为全 0 或全 1 的张量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=63, shape=(2, 3), dtype=float32, numpy=\n",
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]], dtype=float32)>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个矩阵\n",
    "a = tf.ones([2,3]) \n",
    "# 创建一个与 a 形状相同，但是全 0 的新矩阵\n",
    "tf.zeros_like(a) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建与张量A形状一样的全 1 张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=69, shape=(3, 2), dtype=float32, numpy=\n",
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]], dtype=float32)>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个矩阵\n",
    "a = tf.zeros([3,2]) \n",
    "# 创建一个与 a 形状相同，但是全 1 的新矩阵\n",
    "tf.ones_like(a) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建自定义数值张量\n",
    "\n",
    "通过 tf.fill(shape, value)可以创建全为自定义数值 value 的张量，形状由 shape 参数指定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=72, shape=(), dtype=int32, numpy=-1>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建-1 的标量\n",
    "tf.fill([], -1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=75, shape=(1,), dtype=int32, numpy=array([-1])>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建-1 的向量\n",
    "tf.fill([1], -1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=78, shape=(2, 2), dtype=int32, numpy=\n",
       "array([[99, 99],\n",
       "       [99, 99]])>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 2 行 2 列，元素全为 99 的矩阵\n",
    "tf.fill([2,2], 99) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建已知分布的张量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过 tf.random.normal(shape, mean=0.0, stddev=1.0)可以创建形状为 shape，均值为mean，标准差为 stddev 的正态分布$\\mathcal{N}(mean, stddev^2)$。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=84, shape=(2, 2), dtype=float32, numpy=\n",
       "array([[0.4479265 , 1.2336508 ],\n",
       "       [0.96864706, 2.076528  ]], dtype=float32)>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建标准正态分布的张量\n",
    "tf.random.normal([2,2]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=90, shape=(2, 2), dtype=float32, numpy=\n",
       "array([[ 0.06462109,  1.8899262 ],\n",
       "       [-1.3979323 ,  0.07801807]], dtype=float32)>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建均值为 1，标准差为 2 的正态分布的张量\n",
    "tf.random.normal([2,2], mean=1,stddev=2) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过 tf.random.uniform(shape, minval=0, maxval=None, dtype=tf.float32)可以创建采样自[minval, maxval)区间的均匀分布的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=97, shape=(3, 2), dtype=float32, numpy=\n",
       "array([[0.5197921 , 0.80691314],\n",
       "       [0.38051474, 0.05250001],\n",
       "       [0.18329549, 0.8741617 ]], dtype=float32)>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建采样自[0,1)均匀分布的矩阵\n",
    "tf.random.uniform([3,2]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=104, shape=(2, 2), dtype=float32, numpy=\n",
       "array([[0.4374528 , 7.3671246 ],\n",
       "       [2.1262336 , 0.12584329]], dtype=float32)>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建采样自[0,10)均匀分布的矩阵\n",
    "tf.random.uniform([2,2],maxval=10) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果需要均匀采样整形类型的数据，必须指定采样区间的最大值 maxval 参数，同时指定数据类型为 tf.int*型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=108, shape=(2, 2), dtype=int32, numpy=\n",
       "array([[15, 50],\n",
       "       [38, 84]])>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建采样自[0,100)均匀分布的整型矩阵\n",
    "tf.random.uniform([2,2],maxval=100,dtype=tf.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建序列\n",
    "\n",
    "tf.range(limit, delta=1)可以创建[0, limit)之间，步长为 delta 的整型序列，不包含 limit 本身。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=112, shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0~10，不包含 10\n",
    "tf.range(10) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=116, shape=(5,), dtype=int32, numpy=array([0, 2, 4, 6, 8])>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 0~10，步长为 2 的整形序列\n",
    "tf.range(10,delta=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=120, shape=(5,), dtype=int32, numpy=array([1, 3, 5, 7, 9])>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.range(1,10,delta=2) # 1~10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 张量的典型应用\n",
    "\n",
    "### 标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(0.28147948, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# 随机模拟网络输出\n",
    "out = tf.random.uniform([4,10]) \n",
    "# 随机构造样本真实标签\n",
    "y = tf.constant([2,3,2,0]) \n",
    "# one-hot 编码\n",
    "y = tf.one_hot(y, depth=10) \n",
    "# 计算每个样本的 MSE\n",
    "loss = tf.keras.losses.mse(y, out) \n",
    "# 平均 MSE,loss 应是标量\n",
    "loss = tf.reduce_mean(loss) \n",
    "print(loss)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 向量\n",
    "\n",
    "考虑 2 个输出节点的网络层， 我们创建长度为 2 的偏置向量b，并累加在每个输出节点上："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=147, shape=(4, 2), dtype=float32, numpy=\n",
       "array([[-2.004388  , -0.30366704],\n",
       "       [ 0.5251593 ,  0.61763936],\n",
       "       [ 1.1223725 , -2.277926  ],\n",
       "       [-0.20396625, -0.31192765]], dtype=float32)>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# z=wx,模拟获得激活函数的输入 z\n",
    "z = tf.random.normal([4,2])\n",
    "# 创建偏置向量\n",
    "b = tf.zeros([2])\n",
    "# 累加上偏置向量\n",
    "z = z + b \n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建输入节点数为 4，输出节点数为 3 的线性层网络，那么它的偏置向量 b 的长度应为 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一层 Wx+b，输出节点为 3\n",
    "fc = tf.keras.layers.Dense(3) \n",
    "# 通过 build 函数创建 W,b 张量，输入节点为 4\n",
    "fc.build(input_shape=(2,4))\n",
    "# 查看偏置向量\n",
    "fc.bias "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=185, shape=(2, 3), dtype=float32, numpy=\n",
       "array([[ 3.4116697,  3.4116697,  3.4116697],\n",
       "       [-1.0482166, -1.0482166, -1.0482166]], dtype=float32)>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2 个样本，特征长度为 4 的张量\n",
    "x = tf.random.normal([2,4]) \n",
    "# 定义 W 张量\n",
    "w = tf.ones([4,3])\n",
    "# 定义 b 张量\n",
    "b = tf.zeros([3]) \n",
    "# X@W+b 运算\n",
    "o = x@w+b \n",
    "o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'kernel:0' shape=(4, 3) dtype=float32, numpy=\n",
       "array([[-0.00832933,  0.3129425 ,  0.05450684],\n",
       "       [ 0.4455037 ,  0.8942138 ,  0.19051397],\n",
       "       [ 0.18969214, -0.43504652, -0.8300707 ],\n",
       "       [ 0.6065289 ,  0.33857238, -0.15131074]], dtype=float32)>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义全连接层的输出节点为 3\n",
    "fc = tf.keras.layers.Dense(3) \n",
    "# 定义全连接层的输入节点为 4\n",
    "fc.build(input_shape=(2,4)) \n",
    "# 查看权值矩阵 W\n",
    "fc.kernel "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三维张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(25000, 80)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自动加载 IMDB 电影评价数据集\n",
    "(x_train,y_train),(x_test,y_test)=keras.datasets.imdb.load_data(num_words=10000)\n",
    "# 将句子填充、截断为等长 80 个单词的句子\n",
    "x_train = keras.preprocessing.sequence.pad_sequences(x_train,maxlen=80)\n",
    "x_train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到 x_train 张量的 shape 为[25000,80]，其中 25000 表示句子个数， 80 表示每个句子共 80 个单词，每个单词使用数字编码方式表示。\n",
    "\n",
    "我们通过 layers.Embedding 层将数字编码的单词转换为长度为 100 个词向量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorShape([25000, 80, 100])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建词向量 Embedding 层类\n",
    "embedding = tf.keras.layers.Embedding(10000, 100)\n",
    "# 将数字编码的单词转换为词向量\n",
    "out = embedding(x_train)\n",
    "out.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，经过 Embedding 层编码后，句子张量的 shape 变为[25000,80,100]，其中 100 表示每个单词编码为长度是 100 的向量。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 四维张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorShape([4, 30, 30, 16])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建 32x32 的彩色图片输入，个数为 4\n",
    "x = tf.random.normal([4,32,32,3])\n",
    "# 创建卷积神经网络\n",
    "layer = layers.Conv2D(16, kernel_size=3)\n",
    "# 前向计算\n",
    "out = layer(x) \n",
    "# 输出大小\n",
    "out.shape "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorShape([3, 3, 3, 16])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问卷积核张量\n",
    "layer.kernel.shape "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引与切片\n",
    "### 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建4维张量\n",
    "x = tf.random.normal([4,32,32,3]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=267, shape=(32, 32, 3), dtype=float32, numpy=\n",
       "array([[[-7.0219427e-01, -2.3099621e-01,  1.2094800e+00],\n",
       "        [-2.6357755e-01, -7.0312130e-01,  1.5127902e+00],\n",
       "        [ 1.9542392e+00, -6.0328948e-01,  2.3562982e+00],\n",
       "        ...,\n",
       "        [ 8.0949664e-02,  1.3448699e+00,  1.5983492e+00],\n",
       "        [ 6.1380845e-01, -1.7244936e+00, -4.7602654e-01],\n",
       "        [ 3.7072542e-01, -9.3134858e-02,  1.0290160e+00]],\n",
       "\n",
       "       [[-7.1262009e-02, -1.9358450e+00, -3.8081226e-01],\n",
       "        [ 4.0250048e-01,  1.6657301e+00,  6.8908399e-01],\n",
       "        [ 6.0066265e-01, -1.8154552e+00,  7.2157127e-01],\n",
       "        ...,\n",
       "        [ 9.7958475e-01, -4.7344011e-01,  5.3237152e-01],\n",
       "        [ 2.1606743e-01,  8.5561192e-01,  1.9893669e+00],\n",
       "        [-1.5704334e+00,  2.2452128e+00, -6.6826850e-01]],\n",
       "\n",
       "       [[ 8.7351984e-01,  1.1367199e+00,  1.6051801e+00],\n",
       "        [-1.5495791e+00,  8.7256275e-02, -1.4450808e+00],\n",
       "        [ 2.7992409e-03,  9.2620146e-01,  3.1401116e-02],\n",
       "        ...,\n",
       "        [-1.1206126e+00,  1.0360395e+00, -7.4580491e-01],\n",
       "        [-8.1751364e-01, -1.0892615e+00, -9.9637800e-01],\n",
       "        [-1.1539967e+00,  2.1365585e+00, -1.1069869e+00]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[ 2.3596993e-01,  2.9985669e-01,  6.2061930e-01],\n",
       "        [ 7.3106623e-01, -1.7183633e+00,  2.3750028e-01],\n",
       "        [-4.3456620e-01, -1.0403070e+00,  6.9233239e-01],\n",
       "        ...,\n",
       "        [-4.7624469e-01, -7.1421218e-01, -6.5535909e-01],\n",
       "        [ 7.7424002e-01, -1.1215406e-01,  2.1705101e-01],\n",
       "        [ 2.3676555e-01, -5.6109911e-01,  6.1984509e-01]],\n",
       "\n",
       "       [[-2.2387651e-01, -1.4536206e+00, -9.0143257e-01],\n",
       "        [-9.6634254e-02, -1.3626976e+00, -8.9061216e-02],\n",
       "        [ 7.8392905e-01, -8.5201168e-01, -1.0795475e+00],\n",
       "        ...,\n",
       "        [ 7.2759771e-01, -1.0666396e-01, -8.0227345e-01],\n",
       "        [ 1.6749691e+00,  1.6169232e-01, -2.6963761e+00],\n",
       "        [-2.4894271e+00, -3.1353953e+00, -1.8724644e+00]],\n",
       "\n",
       "       [[-5.4862887e-01,  4.1988530e-04,  1.0539432e+00],\n",
       "        [ 5.9619349e-01,  4.0080160e-01,  1.6369225e+00],\n",
       "        [ 3.4577203e-01,  5.4301182e-03,  1.3783123e-01],\n",
       "        ...,\n",
       "        [ 7.8426637e-03,  3.3398920e-01, -1.3126589e+00],\n",
       "        [ 6.0817027e-01, -1.0952499e+00,  7.5046889e-02],\n",
       "        [-1.2853276e+00,  1.9734532e-01,  9.2883229e-02]]], dtype=float32)>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第 1 张图片的数据\n",
    "x[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=275, shape=(32, 3), dtype=float32, numpy=\n",
       "array([[-0.07126201, -1.935845  , -0.38081226],\n",
       "       [ 0.40250048,  1.6657301 ,  0.689084  ],\n",
       "       [ 0.60066265, -1.8154552 ,  0.72157127],\n",
       "       [-0.18605961,  0.3215979 , -0.72414666],\n",
       "       [ 1.2760842 ,  1.3075244 ,  2.2132673 ],\n",
       "       [-0.1804619 , -1.291914  ,  1.1924539 ],\n",
       "       [-0.39199767,  0.07842433, -1.4994006 ],\n",
       "       [ 0.44130135,  0.26472968,  0.430258  ],\n",
       "       [-0.73169357, -0.28534532,  1.2435484 ],\n",
       "       [ 0.1805095 ,  0.37666783, -0.65572983],\n",
       "       [ 0.8357486 , -0.23406585,  0.05083642],\n",
       "       [ 0.7514649 , -1.7395717 , -0.76412684],\n",
       "       [-0.4636954 , -0.75089204, -0.35915136],\n",
       "       [-0.33544475,  1.3860176 ,  0.36219397],\n",
       "       [-0.12744768,  0.23773319,  1.4616114 ],\n",
       "       [-1.2741249 , -0.47133496,  0.08080819],\n",
       "       [-0.1396601 ,  0.38350615, -0.6777555 ],\n",
       "       [-0.00657463, -0.57194585, -0.95212233],\n",
       "       [ 0.4648831 , -0.8423635 ,  0.7992526 ],\n",
       "       [ 1.4286498 , -0.07325545,  0.01996386],\n",
       "       [-0.02531292, -0.10882495,  1.3406302 ],\n",
       "       [-1.0866838 , -0.7082561 ,  0.80347764],\n",
       "       [-1.1048872 , -0.33491465,  0.09116811],\n",
       "       [ 2.107919  , -1.9411707 ,  0.5720035 ],\n",
       "       [ 0.28578573,  0.03755545, -0.68451494],\n",
       "       [ 0.68931335,  0.4966916 , -0.2771673 ],\n",
       "       [-0.42836922, -0.6080326 , -3.181805  ],\n",
       "       [-1.0030181 ,  1.3312383 , -0.9009765 ],\n",
       "       [ 0.6638572 ,  1.5113953 ,  0.335878  ],\n",
       "       [ 0.97958475, -0.4734401 ,  0.5323715 ],\n",
       "       [ 0.21606743,  0.8556119 ,  1.9893669 ],\n",
       "       [-1.5704334 ,  2.2452128 , -0.6682685 ]], dtype=float32)>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第 1 张图片的第 2 行\n",
    "x[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=287, shape=(3,), dtype=float32, numpy=array([ 0.60066265, -1.8154552 ,  0.72157127], dtype=float32)>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第 1 张图片，第 2 行，第 3 列的数据\n",
    "x[0][1][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=303, shape=(), dtype=float32, numpy=-0.32487488>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第 3 张图片，第 2 行，第 1 列的像素， B 通道(第 2 个通道)颜色强度值\n",
    "x[2][1][0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=307, shape=(3,), dtype=float32, numpy=array([-0.825554  , -0.57677746, -1.5886215 ], dtype=float32)>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第 2 张图片，第 10 行，第 3 列的数据\n",
    "x[1,9,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=311, shape=(2, 32, 32, 3), dtype=float32, numpy=\n",
       "array([[[[-0.27065626,  0.04507438, -1.3989624 ],\n",
       "         [-0.38363087, -0.8414309 , -0.5714444 ],\n",
       "         [ 0.9970737 , -0.507904  , -3.054791  ],\n",
       "         ...,\n",
       "         [ 0.9752535 ,  0.43121856,  0.5449514 ],\n",
       "         [-1.3020327 ,  1.404531  , -0.85069233],\n",
       "         [ 1.6810054 ,  1.947512  , -0.73379064]],\n",
       "\n",
       "        [[ 1.250201  , -1.2661483 ,  0.28089526],\n",
       "         [-0.16203284, -1.7313437 , -0.11586773],\n",
       "         [ 0.01166931,  0.25535068, -0.7980113 ],\n",
       "         ...,\n",
       "         [ 0.7994417 , -1.4206645 ,  0.825179  ],\n",
       "         [ 3.3514338 , -0.6262688 , -0.54112184],\n",
       "         [-1.1126224 , -0.20762184, -0.37852097]],\n",
       "\n",
       "        [[-0.714908  ,  0.05437288,  0.30103   ],\n",
       "         [ 0.13863057,  1.9148612 , -0.22270656],\n",
       "         [ 0.77030367, -0.8987363 , -2.271226  ],\n",
       "         ...,\n",
       "         [ 0.48259643,  0.46535566,  0.33530015],\n",
       "         [-0.11276385,  1.6014396 ,  2.0770946 ],\n",
       "         [ 0.09005   ,  0.42257673,  0.10470365]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 0.07619269, -1.1428185 , -1.8437898 ],\n",
       "         [ 1.8154156 , -1.5669407 , -0.60207915],\n",
       "         [ 0.582216  , -0.31015992, -0.98042357],\n",
       "         ...,\n",
       "         [-0.02729896,  0.18838185,  1.1153767 ],\n",
       "         [ 1.6635106 , -0.76928335,  0.18232371],\n",
       "         [ 1.0622841 , -0.86671835,  0.3345515 ]],\n",
       "\n",
       "        [[-1.5433956 ,  0.02137185,  1.7347324 ],\n",
       "         [-1.8482593 ,  0.5477099 ,  0.88056016],\n",
       "         [ 0.5630456 ,  0.18856488, -0.74677515],\n",
       "         ...,\n",
       "         [ 0.37370232, -0.6497492 ,  1.4659142 ],\n",
       "         [ 0.44234493,  1.6022148 , -0.38827336],\n",
       "         [ 0.46496433,  1.0463634 , -2.4259932 ]],\n",
       "\n",
       "        [[ 1.135657  ,  1.2218912 , -1.1557935 ],\n",
       "         [-1.9986897 , -0.57883817,  1.5158479 ],\n",
       "         [ 0.3290811 , -0.24894299, -2.0863893 ],\n",
       "         ...,\n",
       "         [ 0.58293784, -0.2604069 , -1.9020457 ],\n",
       "         [ 0.04737408, -0.85599065,  0.4549411 ],\n",
       "         [-0.08805733, -0.350059  , -0.07065321]]],\n",
       "\n",
       "\n",
       "       [[[ 0.1762981 ,  1.9227296 , -1.1046643 ],\n",
       "         [-0.11788968, -0.37694454, -0.49621934],\n",
       "         [ 0.7211055 , -0.8875129 ,  0.75349295],\n",
       "         ...,\n",
       "         [ 0.6382744 , -0.85995555, -0.97777   ],\n",
       "         [ 0.41687196, -1.468904  ,  0.24110301],\n",
       "         [ 0.25309828,  0.18402308,  2.6470308 ]],\n",
       "\n",
       "        [[ 0.7140523 , -0.32487488,  0.08518142],\n",
       "         [ 0.19374739, -0.9737764 ,  0.6167453 ],\n",
       "         [-0.9677349 , -0.5899758 ,  0.21954292],\n",
       "         ...,\n",
       "         [-0.25765333, -0.8441168 , -0.10940843],\n",
       "         [-0.40662497,  0.4549619 , -2.9624207 ],\n",
       "         [-0.26953518, -0.07413862,  0.43645984]],\n",
       "\n",
       "        [[ 0.38168404,  1.0600123 ,  1.0865871 ],\n",
       "         [ 0.2212861 , -0.2527501 ,  0.67545956],\n",
       "         [-0.38353726,  1.6133431 , -1.1224511 ],\n",
       "         ...,\n",
       "         [-2.1471686 , -0.62935656, -0.14734352],\n",
       "         [-0.16033861, -0.581677  ,  0.8779897 ],\n",
       "         [ 0.5405631 ,  0.47561133,  1.3956453 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 0.47887114,  1.8761681 ,  0.6558065 ],\n",
       "         [-0.160998  ,  0.32347536, -0.10665331],\n",
       "         [ 1.1342022 ,  1.6861796 ,  0.28839332],\n",
       "         ...,\n",
       "         [ 0.0121064 , -0.19840282, -0.5072944 ],\n",
       "         [-0.04775456,  0.51710296, -0.41017172],\n",
       "         [ 0.88649493,  1.2686044 ,  0.46773624]],\n",
       "\n",
       "        [[ 1.2657948 , -0.2184185 ,  1.8141799 ],\n",
       "         [-0.374493  ,  0.10619959,  1.3309448 ],\n",
       "         [ 0.2219191 , -0.6368278 , -1.4083108 ],\n",
       "         ...,\n",
       "         [ 1.319825  ,  3.0516157 ,  0.78542894],\n",
       "         [-0.4946238 , -2.1223729 ,  0.46084297],\n",
       "         [-1.5520899 , -1.0373405 , -0.8841994 ]],\n",
       "\n",
       "        [[ 0.8850192 ,  0.96342975,  1.8899171 ],\n",
       "         [ 0.775327  , -0.8536737 , -0.5784483 ],\n",
       "         [ 0.61005706,  1.0998356 ,  0.4594374 ],\n",
       "         ...,\n",
       "         [-0.9080218 , -0.08965956, -0.7850701 ],\n",
       "         [ 1.2477832 , -0.9169083 , -1.3263239 ],\n",
       "         [ 1.0170643 , -0.15848164,  0.47694787]]]], dtype=float32)>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取第 2,3 张图片\n",
    "x[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=315, shape=(32, 32, 3), dtype=float32, numpy=\n",
       "array([[[-7.0219427e-01, -2.3099621e-01,  1.2094800e+00],\n",
       "        [-2.6357755e-01, -7.0312130e-01,  1.5127902e+00],\n",
       "        [ 1.9542392e+00, -6.0328948e-01,  2.3562982e+00],\n",
       "        ...,\n",
       "        [ 8.0949664e-02,  1.3448699e+00,  1.5983492e+00],\n",
       "        [ 6.1380845e-01, -1.7244936e+00, -4.7602654e-01],\n",
       "        [ 3.7072542e-01, -9.3134858e-02,  1.0290160e+00]],\n",
       "\n",
       "       [[-7.1262009e-02, -1.9358450e+00, -3.8081226e-01],\n",
       "        [ 4.0250048e-01,  1.6657301e+00,  6.8908399e-01],\n",
       "        [ 6.0066265e-01, -1.8154552e+00,  7.2157127e-01],\n",
       "        ...,\n",
       "        [ 9.7958475e-01, -4.7344011e-01,  5.3237152e-01],\n",
       "        [ 2.1606743e-01,  8.5561192e-01,  1.9893669e+00],\n",
       "        [-1.5704334e+00,  2.2452128e+00, -6.6826850e-01]],\n",
       "\n",
       "       [[ 8.7351984e-01,  1.1367199e+00,  1.6051801e+00],\n",
       "        [-1.5495791e+00,  8.7256275e-02, -1.4450808e+00],\n",
       "        [ 2.7992409e-03,  9.2620146e-01,  3.1401116e-02],\n",
       "        ...,\n",
       "        [-1.1206126e+00,  1.0360395e+00, -7.4580491e-01],\n",
       "        [-8.1751364e-01, -1.0892615e+00, -9.9637800e-01],\n",
       "        [-1.1539967e+00,  2.1365585e+00, -1.1069869e+00]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[ 2.3596993e-01,  2.9985669e-01,  6.2061930e-01],\n",
       "        [ 7.3106623e-01, -1.7183633e+00,  2.3750028e-01],\n",
       "        [-4.3456620e-01, -1.0403070e+00,  6.9233239e-01],\n",
       "        ...,\n",
       "        [-4.7624469e-01, -7.1421218e-01, -6.5535909e-01],\n",
       "        [ 7.7424002e-01, -1.1215406e-01,  2.1705101e-01],\n",
       "        [ 2.3676555e-01, -5.6109911e-01,  6.1984509e-01]],\n",
       "\n",
       "       [[-2.2387651e-01, -1.4536206e+00, -9.0143257e-01],\n",
       "        [-9.6634254e-02, -1.3626976e+00, -8.9061216e-02],\n",
       "        [ 7.8392905e-01, -8.5201168e-01, -1.0795475e+00],\n",
       "        ...,\n",
       "        [ 7.2759771e-01, -1.0666396e-01, -8.0227345e-01],\n",
       "        [ 1.6749691e+00,  1.6169232e-01, -2.6963761e+00],\n",
       "        [-2.4894271e+00, -3.1353953e+00, -1.8724644e+00]],\n",
       "\n",
       "       [[-5.4862887e-01,  4.1988530e-04,  1.0539432e+00],\n",
       "        [ 5.9619349e-01,  4.0080160e-01,  1.6369225e+00],\n",
       "        [ 3.4577203e-01,  5.4301182e-03,  1.3783123e-01],\n",
       "        ...,\n",
       "        [ 7.8426637e-03,  3.3398920e-01, -1.3126589e+00],\n",
       "        [ 6.0817027e-01, -1.0952499e+00,  7.5046889e-02],\n",
       "        [-1.2853276e+00,  1.9734532e-01,  9.2883229e-02]]], dtype=float32)>"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取第一张图片\n",
    "x[0,::] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=319, shape=(4, 14, 14, 3), dtype=float32, numpy=\n",
       "array([[[[-7.02194273e-01, -2.30996206e-01,  1.20948005e+00],\n",
       "         [ 1.95423925e+00, -6.03289485e-01,  2.35629821e+00],\n",
       "         [-2.71816939e-01,  4.05274034e-01,  5.08846402e-01],\n",
       "         ...,\n",
       "         [ 1.69007957e-01,  9.71024454e-01, -5.70908725e-01],\n",
       "         [ 1.41010773e+00,  5.06117880e-01,  2.77412999e-02],\n",
       "         [ 2.49900296e-01, -1.49797714e+00,  8.30788240e-02]],\n",
       "\n",
       "        [[ 8.73519838e-01,  1.13671994e+00,  1.60518014e+00],\n",
       "         [ 2.79924087e-03,  9.26201463e-01,  3.14011164e-02],\n",
       "         [ 3.35851945e-02,  1.70119077e-01,  5.38629070e-02],\n",
       "         ...,\n",
       "         [-7.83237293e-02, -1.26492202e+00, -1.16074145e-01],\n",
       "         [-1.33017492e+00,  1.24992168e+00,  1.05541384e+00],\n",
       "         [-1.45211816e-01,  2.00162888e+00, -1.27495480e+00]],\n",
       "\n",
       "        [[ 5.31196654e-01, -8.22054625e-01,  7.94631481e-01],\n",
       "         [ 1.30485564e-01,  2.09942803e-01,  9.42976356e-01],\n",
       "         [-7.93929577e-01, -2.37039421e-02,  1.90293765e+00],\n",
       "         ...,\n",
       "         [ 6.11670017e-01, -9.27838743e-01, -2.39658341e-01],\n",
       "         [-8.34900439e-01, -1.09002911e-01, -2.63239652e-01],\n",
       "         [ 1.35091627e+00, -1.28201866e+00, -1.34906209e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 2.35495061e-01, -2.46862695e-01, -6.86209321e-01],\n",
       "         [-5.42564929e-01, -6.34594142e-01,  2.03790903e+00],\n",
       "         [-8.82508397e-01,  1.45494258e+00, -1.57170427e+00],\n",
       "         ...,\n",
       "         [-2.26400948e+00, -7.28164434e-01,  2.28254065e-01],\n",
       "         [ 1.14805412e+00, -6.00487530e-01, -1.23350441e+00],\n",
       "         [-1.57821524e+00, -8.72424066e-01, -3.61845285e-01]],\n",
       "\n",
       "        [[ 8.70905697e-01,  1.31709099e-01,  8.52394879e-01],\n",
       "         [ 2.85219043e-01, -1.42846286e-01, -6.44049514e-03],\n",
       "         [-3.08485717e-01, -6.12259686e-01,  1.05729774e-01],\n",
       "         ...,\n",
       "         [-2.04418302e+00,  5.91382802e-01, -6.59465253e-01],\n",
       "         [ 3.04451119e-02, -8.88049006e-01,  1.91191423e+00],\n",
       "         [ 1.11219697e-01,  4.04905677e-01,  1.09833276e+00]],\n",
       "\n",
       "        [[-2.90043145e-01, -6.49995863e-01, -9.41093087e-01],\n",
       "         [-2.16698313e+00,  4.11937386e-01, -3.05613279e-01],\n",
       "         [ 2.74954021e-01,  1.10240984e+00, -1.10876463e-01],\n",
       "         ...,\n",
       "         [ 8.51374045e-02, -4.08442050e-01,  1.19229674e+00],\n",
       "         [ 1.06518853e+00,  3.94495815e-01,  1.08041584e+00],\n",
       "         [-3.05404186e-01,  1.29312861e+00, -1.17902529e+00]]],\n",
       "\n",
       "\n",
       "       [[[-2.70656258e-01,  4.50743809e-02, -1.39896238e+00],\n",
       "         [ 9.97073710e-01, -5.07903993e-01, -3.05479097e+00],\n",
       "         [-2.24699974e+00,  1.19728541e+00,  8.13158631e-01],\n",
       "         ...,\n",
       "         [ 6.93736196e-01, -3.33661914e-01, -1.67671785e-01],\n",
       "         [ 5.11429846e-01, -7.76259661e-01,  6.93163753e-01],\n",
       "         [-1.00238740e+00, -4.98431958e-02, -3.31748992e-01]],\n",
       "\n",
       "        [[-7.14908004e-01,  5.43728769e-02,  3.01030010e-01],\n",
       "         [ 7.70303667e-01, -8.98736298e-01, -2.27122593e+00],\n",
       "         [-1.01220095e+00, -2.19669676e+00,  1.07996321e+00],\n",
       "         ...,\n",
       "         [ 1.06403923e+00, -4.18804586e-01, -5.65670803e-02],\n",
       "         [ 3.97945613e-01,  2.88128376e-01,  8.53033364e-01],\n",
       "         [ 2.01265901e-01,  6.09949157e-02,  1.10615969e+00]],\n",
       "\n",
       "        [[-1.01147890e+00, -2.21185422e+00, -6.83018982e-01],\n",
       "         [-1.19919753e+00, -1.13069928e+00,  7.48360515e-01],\n",
       "         [ 9.00967538e-01,  1.24687731e+00, -7.87668765e-01],\n",
       "         ...,\n",
       "         [-1.39800751e+00, -5.19660115e-01, -3.96768540e-01],\n",
       "         [-3.97496879e-01,  9.58673835e-01, -2.74695158e-01],\n",
       "         [-6.69749796e-01, -1.93741226e+00,  1.62042308e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 2.92388296e+00, -1.27406761e-01, -1.21746266e+00],\n",
       "         [-2.37035245e-01, -7.67136514e-01,  6.60005748e-01],\n",
       "         [-1.17618394e+00,  1.02504003e+00,  1.12616690e-02],\n",
       "         ...,\n",
       "         [ 1.81560755e-01,  5.38163662e-01, -1.88821173e+00],\n",
       "         [-6.12107575e-01,  9.78368104e-01, -1.84954619e+00],\n",
       "         [ 5.01245201e-01, -4.36838776e-01,  1.74918139e+00]],\n",
       "\n",
       "        [[-1.45281196e-01, -1.45841050e+00, -1.03161536e-01],\n",
       "         [ 7.88328469e-01, -4.21750635e-01, -5.32041132e-01],\n",
       "         [ 7.75719762e-01,  1.23508036e+00, -1.13122022e+00],\n",
       "         ...,\n",
       "         [ 1.61115742e+00, -8.26767385e-01,  2.75560468e-01],\n",
       "         [-1.10368848e+00, -1.25829995e+00,  1.01926327e+00],\n",
       "         [ 1.70804679e-01,  8.66668150e-02, -2.14904571e+00]],\n",
       "\n",
       "        [[ 4.35236245e-01,  5.94780624e-01, -3.93412411e-01],\n",
       "         [-1.13133931e+00,  1.86710310e+00, -1.26194072e+00],\n",
       "         [ 7.39620149e-01,  1.40815067e+00, -1.33006942e+00],\n",
       "         ...,\n",
       "         [-7.80119061e-01, -1.65343928e+00,  3.07866037e-01],\n",
       "         [ 1.03549993e+00,  1.44099224e+00, -1.34135687e+00],\n",
       "         [ 5.61135598e-02,  1.84003264e-01,  7.96238661e-01]]],\n",
       "\n",
       "\n",
       "       [[[ 1.76298097e-01,  1.92272961e+00, -1.10466433e+00],\n",
       "         [ 7.21105516e-01, -8.87512922e-01,  7.53492951e-01],\n",
       "         [-2.96618007e-02,  5.06691575e-01,  6.82835519e-01],\n",
       "         ...,\n",
       "         [ 1.15614605e+00, -8.94406021e-01, -1.48345932e-01],\n",
       "         [-1.32765901e+00, -3.42463225e-01,  2.16877192e-01],\n",
       "         [ 1.57663673e-01, -1.40034568e+00, -1.97294724e+00]],\n",
       "\n",
       "        [[ 3.81684035e-01,  1.06001234e+00,  1.08658707e+00],\n",
       "         [-3.83537263e-01,  1.61334312e+00, -1.12245107e+00],\n",
       "         [-1.83346283e+00, -2.47803062e-01,  2.21341825e+00],\n",
       "         ...,\n",
       "         [ 4.05869216e-01,  8.62570584e-01, -7.76962757e-01],\n",
       "         [-1.52430624e-01,  4.15550381e-01,  3.06763500e-01],\n",
       "         [-1.62499893e+00, -1.06583202e+00,  4.93914299e-02]],\n",
       "\n",
       "        [[-5.79081178e-01, -1.13052237e+00,  1.50933599e+00],\n",
       "         [ 8.74713063e-02,  1.44425377e-01, -2.33173609e+00],\n",
       "         [-1.26265538e+00,  2.56226003e-01, -2.06483126e-01],\n",
       "         ...,\n",
       "         [ 2.20740989e-01,  1.17387199e+00, -5.22666812e-01],\n",
       "         [ 8.47897410e-01, -2.50573874e+00,  4.76146489e-02],\n",
       "         [-1.22420006e-01,  1.52002394e-01,  7.63378978e-01]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-7.66142309e-01, -2.76933134e-01, -4.76586491e-01],\n",
       "         [-6.60026133e-01, -1.49866867e+00,  8.75326633e-01],\n",
       "         [-6.68435097e-01,  2.26192832e+00, -1.22474086e+00],\n",
       "         ...,\n",
       "         [ 2.92570307e-03, -8.59412849e-01, -6.53931081e-01],\n",
       "         [-1.24378884e+00,  1.32344520e+00, -6.67898655e-01],\n",
       "         [-3.76960963e-01, -9.00849462e-01,  1.91332281e+00]],\n",
       "\n",
       "        [[-1.28930843e+00, -8.30998063e-01, -1.27104342e+00],\n",
       "         [-5.41054569e-02,  1.23388439e-01,  8.60991031e-02],\n",
       "         [ 1.09341478e+00, -3.41416985e-01, -1.56087899e+00],\n",
       "         ...,\n",
       "         [-2.23110127e+00, -2.55362415e+00,  1.68435347e+00],\n",
       "         [ 2.12327372e-02,  7.80173600e-01, -1.27830505e+00],\n",
       "         [-2.00047040e+00, -7.36672580e-01, -3.78768414e-01]],\n",
       "\n",
       "        [[ 5.23962259e-01, -4.18583989e-01, -2.05825615e+00],\n",
       "         [ 7.44012296e-01, -1.91733336e+00, -7.79818952e-01],\n",
       "         [-7.68631518e-01, -7.31598258e-01,  2.53575947e-02],\n",
       "         ...,\n",
       "         [-6.49754584e-01,  6.63033247e-01, -6.26816213e-01],\n",
       "         [-7.58364737e-01,  1.60427403e+00, -6.45411849e-01],\n",
       "         [-7.39997804e-01, -8.10026526e-02, -6.07126355e-01]]],\n",
       "\n",
       "\n",
       "       [[[-4.49624330e-01, -6.45022631e-01,  4.53030348e-01],\n",
       "         [-1.84494126e+00, -2.02849165e-01, -3.49523008e-01],\n",
       "         [-1.47210538e-01, -5.61328173e-01, -5.95166802e-01],\n",
       "         ...,\n",
       "         [-5.38687289e-01, -2.38861859e-01,  6.99575007e-01],\n",
       "         [ 2.43123412e-01,  1.73608792e+00,  5.77304423e-01],\n",
       "         [ 1.65295899e+00, -3.28181356e-01, -1.85934830e+00]],\n",
       "\n",
       "        [[ 8.49936754e-02, -3.99062961e-01,  1.01641095e+00],\n",
       "         [-4.64936614e-01,  1.97114319e-01,  1.22489941e+00],\n",
       "         [-1.57250002e-01,  4.89614934e-01, -6.44612372e-01],\n",
       "         ...,\n",
       "         [ 2.71663368e-01, -1.77954948e+00,  8.22196841e-01],\n",
       "         [-2.89494991e-01,  1.66179204e+00,  2.64826274e+00],\n",
       "         [ 9.02917445e-01,  7.03525662e-01, -8.47606063e-02]],\n",
       "\n",
       "        [[ 1.08679748e+00,  2.46698260e+00,  7.52660155e-01],\n",
       "         [ 1.46402037e+00, -5.41026115e-01, -4.17117149e-01],\n",
       "         [-1.20526862e+00,  2.81525612e-01,  1.03343165e+00],\n",
       "         ...,\n",
       "         [ 3.52068305e-01,  2.26449347e+00,  7.68602848e-01],\n",
       "         [ 1.06762314e+00,  2.67131686e-01, -6.52104557e-01],\n",
       "         [-5.54667786e-02, -1.71793044e+00, -5.09196341e-01]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-5.02433002e-01,  1.87536880e-01,  1.43357158e-01],\n",
       "         [-1.47620082e+00,  1.09433591e-01, -1.06412089e+00],\n",
       "         [-5.05630612e-01,  7.47303426e-01,  7.25053847e-01],\n",
       "         ...,\n",
       "         [ 8.68164375e-03, -2.05253315e+00, -1.14188397e+00],\n",
       "         [ 1.44829690e-01, -4.06870782e-01,  3.90982300e-01],\n",
       "         [ 2.00468540e+00,  9.63241994e-01,  6.99557960e-01]],\n",
       "\n",
       "        [[-2.99837708e-01,  1.10563600e+00, -2.54646987e-01],\n",
       "         [ 2.19032854e-01,  5.84025860e-01,  1.39980344e-02],\n",
       "         [ 1.81479251e+00,  1.38899148e+00,  1.13903153e+00],\n",
       "         ...,\n",
       "         [ 1.51723170e+00,  3.85772914e-01, -1.01368690e+00],\n",
       "         [-1.94682881e-01,  8.28524947e-01,  2.73723632e-01],\n",
       "         [-4.51957732e-01, -3.39756697e-01, -9.93541360e-01]],\n",
       "\n",
       "        [[ 6.45601332e-01, -4.51286227e-01,  9.63827491e-01],\n",
       "         [ 1.06442976e+00,  1.91181809e-01, -5.40145099e-01],\n",
       "         [ 3.87677401e-01, -5.44734187e-02, -1.16325283e+00],\n",
       "         ...,\n",
       "         [-3.07528287e-01, -4.88106042e-01, -1.16296065e+00],\n",
       "         [-5.76565303e-02, -1.60603690e+00,  1.13691378e+00],\n",
       "         [ 1.55355394e+00,  6.66950405e-01, -6.58418834e-02]]]],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:,0:28:2,0:28:2,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=327, shape=(8,), dtype=int32, numpy=array([8, 7, 6, 5, 4, 3, 2, 1])>"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 考虑一个 0~9 的简单序列向量， 逆序取到第 1 号元素，不包含第 1 号\n",
    "# 创建 0~9 向量\n",
    "x = tf.range(9) \n",
    "# 从 8 取到 0，逆序，不包含 0\n",
    "x[8:0:-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=331, shape=(9,), dtype=int32, numpy=array([8, 7, 6, 5, 4, 3, 2, 1, 0])>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逆序全部元素\n",
    "x[::-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=335, shape=(5,), dtype=int32, numpy=array([8, 6, 4, 2, 0])>"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逆序间隔采样\n",
    "x[::-2] "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取每张图片的所有通道，其中行按着逆序隔行采样，列按着逆序隔行采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=345, shape=(16, 16, 3), dtype=float32, numpy=\n",
       "array([[[-5.63943148e-01,  1.00873506e+00,  1.37100255e+00],\n",
       "        [ 2.96741456e-01, -3.89043003e-01,  1.74590218e+00],\n",
       "        [-5.63913107e-01, -3.69135916e-01,  5.56139767e-01],\n",
       "        [-7.04238176e-01,  1.52065706e+00,  1.19998348e+00],\n",
       "        [ 9.04740512e-01,  5.27634881e-02,  1.65227580e+00],\n",
       "        [-9.63325441e-01,  1.03597417e-01, -5.68294749e-02],\n",
       "        [ 5.81958771e-01,  4.22285646e-01, -4.12933826e-01],\n",
       "        [-2.71714449e-01, -1.02945395e-01, -2.80646890e-01],\n",
       "        [-1.42100644e+00,  8.37074459e-01,  2.78155543e-02],\n",
       "        [-1.53158665e+00, -1.28298402e-01, -9.60409820e-01],\n",
       "        [ 1.79952705e+00, -1.68005481e-01, -1.93054628e+00],\n",
       "        [-1.96579766e+00,  8.93500507e-01, -3.91018629e-01],\n",
       "        [ 1.76559806e-01, -1.10343599e+00,  1.40932286e+00],\n",
       "        [-1.63010859e+00,  2.20892167e+00, -2.81299144e-01],\n",
       "        [ 1.76009250e+00,  1.49996221e+00,  3.10533881e-01],\n",
       "        [ 1.47438809e-01,  5.26952088e-01, -2.58012116e-01]],\n",
       "\n",
       "       [[-3.25570285e-01, -2.11627111e-01,  5.50371230e-01],\n",
       "        [-1.83718705e+00, -9.17835712e-01,  6.61173940e-01],\n",
       "        [-2.09516096e+00, -7.35892802e-02, -1.65230894e+00],\n",
       "        [-2.09755704e-01,  1.11587536e+00, -2.35363066e-01],\n",
       "        [ 2.85442889e-01, -2.40633041e-02, -7.11566091e-01],\n",
       "        [ 7.30711043e-01,  7.49948323e-01, -6.21425271e-01],\n",
       "        [ 1.04104626e+00, -2.06475019e-01,  1.57532132e+00],\n",
       "        [-2.58726746e-01,  3.71670574e-01,  1.09785461e+00],\n",
       "        [ 1.37295270e+00,  2.15636134e+00,  1.93816292e+00],\n",
       "        [-2.90793121e-01, -1.16142726e+00,  1.11004031e+00],\n",
       "        [-9.18091178e-01,  5.37490189e-01, -1.16928779e-01],\n",
       "        [-4.87638175e-01, -2.59460121e-01,  6.90288246e-01],\n",
       "        [-7.22353160e-02,  1.10239971e+00, -1.92188859e-01],\n",
       "        [ 1.10676980e+00, -4.12065953e-01, -2.37038910e-01],\n",
       "        [ 1.01106036e+00, -1.32633829e+00,  3.85407686e-01],\n",
       "        [ 2.39306951e+00, -1.29542232e+00, -5.54799676e-01]],\n",
       "\n",
       "       [[ 7.59828150e-01,  2.54152510e-02, -1.50012159e+00],\n",
       "        [ 1.90302634e+00, -6.19462738e-03,  1.75811201e-01],\n",
       "        [-3.39706719e-01,  7.21950948e-01, -6.24438107e-01],\n",
       "        [-7.81664371e-01,  8.31373692e-01,  2.83479482e-01],\n",
       "        [ 8.53069067e-01,  3.10175955e-01, -9.82245386e-01],\n",
       "        [-2.75143739e-02,  1.32624841e+00,  5.56467652e-01],\n",
       "        [ 8.50600898e-02,  7.38934338e-01, -3.28294963e-01],\n",
       "        [ 3.42702687e-01, -1.03071475e+00,  3.85674760e-02],\n",
       "        [ 2.15324712e+00, -7.46635199e-01,  6.03982151e-01],\n",
       "        [ 1.27023089e+00, -3.14303815e-01, -1.30186319e+00],\n",
       "        [ 1.59010649e+00,  1.41547585e+00, -6.68911099e-01],\n",
       "        [-7.94144511e-01, -1.38270712e+00,  6.47329211e-01],\n",
       "        [-5.51207662e-01,  5.79401255e-01, -6.97782695e-01],\n",
       "        [-1.30820140e-01, -1.38275278e+00,  3.19558889e-01],\n",
       "        [-1.50519288e+00,  2.19885564e+00,  3.28629375e+00],\n",
       "        [ 9.53190103e-02,  2.82347202e-01,  2.88827658e-01]],\n",
       "\n",
       "       [[ 1.10424256e+00, -8.00434411e-01,  9.32684243e-01],\n",
       "        [ 1.42732322e+00, -1.11061084e+00, -2.41685603e-02],\n",
       "        [-7.44086146e-01, -6.35075450e-01, -9.47187424e-01],\n",
       "        [ 3.04003984e-01,  8.55885327e-01,  6.49644375e-01],\n",
       "        [-3.49887065e-03,  3.39116752e-01, -2.60426491e-01],\n",
       "        [ 2.73219198e-01,  4.84456345e-02,  7.42520839e-02],\n",
       "        [-2.89396234e-02,  2.44745374e+00, -1.46668196e-01],\n",
       "        [ 1.52066755e+00, -2.01898247e-01, -1.84484160e+00],\n",
       "        [ 1.56688988e+00, -1.19574010e+00, -9.92278993e-01],\n",
       "        [-9.46493030e-01,  7.28285849e-01,  3.96031708e-01],\n",
       "        [-9.75389481e-01,  1.63219303e-01,  1.43369341e+00],\n",
       "        [ 2.60728931e+00, -7.43419111e-01, -6.61375701e-01],\n",
       "        [ 1.04546225e+00,  3.55479747e-01, -8.37624848e-01],\n",
       "        [ 8.90745819e-02,  7.19225407e-01,  3.81936550e-01],\n",
       "        [ 2.84651071e-01, -1.60964823e+00,  5.21163344e-01],\n",
       "        [ 1.25642455e+00,  2.90397108e-02,  1.56878114e+00]],\n",
       "\n",
       "       [[-1.24428022e+00, -8.07763338e-01,  9.67667460e-01],\n",
       "        [-6.82400882e-01,  1.01535034e+00, -9.78667796e-01],\n",
       "        [ 5.07148564e-01,  7.29072317e-02,  1.83559370e+00],\n",
       "        [-4.55666929e-01, -1.53346944e+00, -1.41682103e-01],\n",
       "        [ 1.08100247e+00, -3.80316854e-01,  1.03086829e+00],\n",
       "        [ 3.14920962e-01,  1.02084291e+00,  2.01196885e+00],\n",
       "        [-1.29434884e+00, -1.84441543e+00, -1.73131418e+00],\n",
       "        [ 1.86066732e-01,  1.28650784e+00, -6.61540747e-01],\n",
       "        [ 8.97417605e-01,  2.10155666e-01, -6.55683756e-01],\n",
       "        [ 7.83864558e-01, -3.07598442e-01,  6.42542779e-01],\n",
       "        [ 2.34714866e+00, -2.01513124e+00, -4.92703944e-01],\n",
       "        [ 8.33278596e-01, -7.13333845e-01, -1.61224276e-01],\n",
       "        [-1.33025062e+00, -1.65143609e-01, -1.79107904e-01],\n",
       "        [ 3.46603811e-01, -1.67564607e+00,  1.10101461e+00],\n",
       "        [-1.30207908e+00, -1.56239927e-01,  1.01381350e+00],\n",
       "        [-4.63981032e-01, -6.25568807e-01, -8.72369528e-01]],\n",
       "\n",
       "       [[-4.77810591e-01, -1.08103800e+00,  2.36154199e-01],\n",
       "        [-4.99762923e-01,  1.87428772e-01, -6.42079949e-01],\n",
       "        [ 9.27673161e-01,  7.40325689e-01,  1.03666437e+00],\n",
       "        [-1.87003171e+00,  1.94166467e-01, -5.82103431e-01],\n",
       "        [-1.38603747e-01, -2.54166412e+00, -3.08083981e-01],\n",
       "        [ 8.29691947e-01, -4.72291321e-01,  7.89855301e-01],\n",
       "        [ 7.24354565e-01, -1.54281139e+00,  1.09281445e+00],\n",
       "        [ 1.59547240e-01, -1.69492757e+00, -1.56126702e+00],\n",
       "        [-7.17902780e-01,  7.02133834e-01, -9.80006039e-01],\n",
       "        [-2.75628060e-01, -1.15468495e-01,  1.30644572e+00],\n",
       "        [ 1.01075304e+00, -7.41704777e-02, -2.54488260e-01],\n",
       "        [ 1.58013117e+00, -1.26707542e+00,  2.82407463e-01],\n",
       "        [ 1.04031825e+00,  8.16366732e-01, -1.01446581e+00],\n",
       "        [-4.72296216e-02, -7.32091010e-01,  2.63138950e-01],\n",
       "        [-6.12152874e-01, -1.26903522e+00, -2.17907619e+00],\n",
       "        [-6.09511137e-01, -6.37994528e-01,  2.59036105e-02]],\n",
       "\n",
       "       [[-1.19012736e-01, -2.06012920e-01,  8.20999324e-01],\n",
       "        [-1.19233169e-01,  1.32799661e+00, -9.73442554e-01],\n",
       "        [ 5.73712528e-01, -1.56145737e-01,  8.13452601e-01],\n",
       "        [-8.49991798e-01, -4.62114185e-01, -1.71133828e+00],\n",
       "        [ 1.51758897e+00,  1.14805412e+00, -1.10360360e+00],\n",
       "        [-7.79439151e-01,  3.44348401e-01, -2.43572831e-01],\n",
       "        [-1.45352292e+00,  2.66507238e-01, -9.30578232e-01],\n",
       "        [ 2.26591490e-02,  3.30673575e-01, -5.88606954e-01],\n",
       "        [ 4.41131592e-01,  2.16908664e-01, -1.74118185e+00],\n",
       "        [-1.68145508e-01,  1.37153602e+00,  3.30091834e-01],\n",
       "        [-6.52610362e-01,  1.52096772e+00,  1.89377403e+00],\n",
       "        [ 1.58308864e+00,  1.29006863e+00, -4.11214292e-01],\n",
       "        [-9.48475361e-01,  7.30038226e-01,  8.15775812e-01],\n",
       "        [ 8.10682952e-01, -4.29345161e-01, -1.10986188e-01],\n",
       "        [-1.44353080e+00,  2.25462750e-01, -6.42599106e-01],\n",
       "        [ 1.04263008e+00,  2.06993556e+00, -7.49897778e-01]],\n",
       "\n",
       "       [[ 1.41442046e-01,  1.18761659e+00, -4.36821342e-01],\n",
       "        [-8.07205439e-02,  1.63004684e+00, -1.19184816e+00],\n",
       "        [-1.36583531e+00,  3.03139472e+00, -8.78197014e-01],\n",
       "        [-1.22691810e+00, -7.47695446e-01,  4.19416636e-01],\n",
       "        [ 7.22835302e-01,  7.45762467e-01,  1.33707690e+00],\n",
       "        [ 2.05093455e+00,  4.65437144e-01,  2.62295492e-02],\n",
       "        [ 6.39067888e-01,  3.16154242e-01,  1.47288907e+00],\n",
       "        [-1.28035903e-01, -3.55690265e+00, -1.58349681e+00],\n",
       "        [ 1.03850633e-01,  1.50644588e+00,  1.38978437e-01],\n",
       "        [-2.08296075e-01,  1.10317922e+00,  1.98035836e-01],\n",
       "        [ 6.88771307e-01,  4.16451871e-01,  5.93916655e-01],\n",
       "        [ 1.59415579e+00,  1.55121136e+00,  7.53430665e-01],\n",
       "        [ 1.00562930e+00,  7.73624241e-01,  3.51739407e-01],\n",
       "        [-8.56186897e-02,  1.19327569e+00,  1.16821980e+00],\n",
       "        [ 4.03111994e-01, -1.17968392e+00,  1.04280043e+00],\n",
       "        [ 4.88940090e-01,  8.60010207e-01, -5.80214202e-01]],\n",
       "\n",
       "       [[ 2.69110322e+00, -2.60688782e-01, -1.70940351e+00],\n",
       "        [ 6.06434584e-01, -2.03603029e-01,  1.93549860e+00],\n",
       "        [ 5.97739875e-01, -3.53138089e-01, -3.95148516e-01],\n",
       "        [ 8.87851477e-01,  1.24851239e+00, -5.99191725e-01],\n",
       "        [ 1.21663952e+00, -1.23810375e+00,  3.84375364e-01],\n",
       "        [ 7.24369824e-01, -2.21712410e-01, -7.69281030e-01],\n",
       "        [ 1.37982297e+00, -4.80857491e-01,  9.02360678e-02],\n",
       "        [-1.19432652e+00,  1.57655156e+00, -5.25479794e-01],\n",
       "        [-1.23722637e+00, -4.73330140e-01,  1.72002006e+00],\n",
       "        [ 9.32200179e-02, -1.63824663e-01,  4.49977451e-05],\n",
       "        [-2.81439632e-01,  5.62410235e-01, -1.54820204e-01],\n",
       "        [ 1.21849978e+00, -9.53745544e-01,  6.56502008e-01],\n",
       "        [-3.58382404e-01, -4.99836922e-01, -9.49684232e-02],\n",
       "        [-1.70188069e+00, -3.56540531e-01,  1.19193387e+00],\n",
       "        [ 1.56105804e+00,  1.67432678e+00,  4.15146679e-01],\n",
       "        [ 5.58234677e-02, -1.16571224e+00,  1.63081431e+00]],\n",
       "\n",
       "       [[ 2.96994001e-01,  2.04504991e+00, -7.27886498e-01],\n",
       "        [ 1.06210876e+00,  1.57655513e+00,  9.85929310e-01],\n",
       "        [-9.19033527e-01,  2.58146763e-01,  1.00481009e+00],\n",
       "        [ 5.28016150e-01, -3.05803895e+00,  1.68863133e-01],\n",
       "        [ 1.74167383e+00, -1.83077073e+00,  4.03199881e-01],\n",
       "        [-1.08501840e+00,  1.67623842e+00, -4.16596204e-01],\n",
       "        [-6.05547011e-01,  4.88634288e-01, -5.50752580e-01],\n",
       "        [ 1.50322735e+00, -1.07728100e+00,  2.55569100e-01],\n",
       "        [-2.36421958e-01,  5.34333467e-01, -2.02792335e+00],\n",
       "        [ 7.01987386e-01,  1.84153724e+00, -3.64045262e-01],\n",
       "        [ 1.30195916e-01, -9.40199345e-02,  2.64073163e-01],\n",
       "        [-6.66917801e-01,  7.85036683e-02, -2.58332849e-01],\n",
       "        [ 1.01366413e+00,  9.33096409e-02, -4.53383952e-01],\n",
       "        [-5.11403501e-01,  4.55686361e-01, -9.26809788e-01],\n",
       "        [ 5.70656955e-01,  1.18871641e+00, -6.50113046e-01],\n",
       "        [ 1.15452051e+00, -9.37806785e-01, -2.45390356e-01]],\n",
       "\n",
       "       [[-9.63364780e-01, -6.38407469e-01,  8.95734787e-01],\n",
       "        [-7.69712627e-01, -2.36510348e+00,  8.57682705e-01],\n",
       "        [ 6.32080138e-01, -2.66448760e+00, -6.02153987e-02],\n",
       "        [ 2.43213058e+00, -1.51404119e+00,  8.58058989e-01],\n",
       "        [-1.84141994e+00,  1.97237635e+00, -1.74248397e+00],\n",
       "        [-9.86508131e-02, -5.00892162e-01, -6.09065235e-01],\n",
       "        [ 1.43583047e+00,  1.18158400e+00,  7.67591774e-01],\n",
       "        [-8.52426052e-01, -4.54574823e-01, -2.22310090e+00],\n",
       "        [ 4.28044468e-01, -1.60001084e-01, -5.11196315e-01],\n",
       "        [-4.01083380e-01, -3.77035439e-01,  1.84484208e+00],\n",
       "        [ 1.38562322e+00, -7.83203483e-01, -1.03224480e+00],\n",
       "        [ 6.93817437e-01, -1.21882983e-01,  7.99068630e-01],\n",
       "        [ 5.46279788e-01,  1.18938553e+00, -7.34234273e-01],\n",
       "        [ 1.17699897e+00, -1.24173212e+00,  3.05417806e-01],\n",
       "        [-6.22690678e-01, -8.45476806e-01, -6.73691809e-01],\n",
       "        [-9.58496690e-01,  1.51849478e-01,  1.65256405e+00]],\n",
       "\n",
       "       [[ 7.78738081e-01,  1.22417808e+00, -1.14153616e-01],\n",
       "        [ 1.76625872e+00,  2.94694602e-01, -1.66483790e-01],\n",
       "        [ 2.08868176e-01, -2.00468135e+00, -1.86807752e-01],\n",
       "        [-6.42797351e-01,  3.71607900e-01,  1.49823055e-01],\n",
       "        [ 4.40657169e-01,  4.17698115e-01, -8.18507299e-02],\n",
       "        [ 8.50629359e-02,  9.12324965e-01,  7.70579502e-02],\n",
       "        [ 1.16332567e+00,  8.15636456e-01, -9.37588930e-01],\n",
       "        [ 1.51588008e-01,  4.39559907e-01, -1.07903689e-01],\n",
       "        [-2.56712437e+00, -1.11898315e+00, -5.63211143e-01],\n",
       "        [-9.68144774e-01,  1.08521485e+00,  5.18362522e-01],\n",
       "        [ 7.65119493e-02,  1.21450767e-01,  6.90626264e-01],\n",
       "        [ 1.56415379e+00, -1.88193128e-01, -4.70561236e-01],\n",
       "        [-1.31334472e+00,  7.26858079e-01,  7.49687910e-01],\n",
       "        [ 6.97770417e-01,  1.90647972e+00,  5.93047023e-01],\n",
       "        [-1.12486744e+00,  4.32820290e-01,  1.62812150e+00],\n",
       "        [-7.29145944e-01,  1.91849530e+00, -6.32217169e-01]],\n",
       "\n",
       "       [[-1.47957325e+00,  1.39177394e+00, -1.57118273e+00],\n",
       "        [ 2.35358149e-01,  2.27529216e+00, -7.38791108e-01],\n",
       "        [ 1.67962420e+00,  1.14057672e+00, -8.48348066e-03],\n",
       "        [-1.00210023e+00,  2.74028897e-01,  3.27582479e-01],\n",
       "        [ 2.77548170e+00, -2.56603155e-02, -3.22584301e-01],\n",
       "        [-7.34580219e-01,  1.85306538e-02,  9.47810531e-01],\n",
       "        [-2.58368820e-01, -1.25770819e+00, -3.38813829e+00],\n",
       "        [ 9.40057755e-01,  1.69211948e+00,  1.22174525e+00],\n",
       "        [ 4.89927769e-01,  2.16845229e-01,  7.40688860e-01],\n",
       "        [-8.53393495e-01,  1.24633753e+00,  1.01721227e+00],\n",
       "        [-1.04553258e+00, -7.86599755e-01, -8.81558537e-01],\n",
       "        [-2.02061936e-01, -1.33151448e+00,  1.28779542e+00],\n",
       "        [ 1.35033977e+00, -7.07781971e-01,  4.30042773e-01],\n",
       "        [ 8.94449115e-01,  9.23492670e-01,  6.98308468e-01],\n",
       "        [ 1.05618548e+00,  1.24459910e+00, -1.31749406e-01],\n",
       "        [-6.54686093e-01, -2.76859581e-01, -1.04207862e-02]],\n",
       "\n",
       "       [[-4.76453066e-01, -2.99547255e-01, -5.96116364e-01],\n",
       "        [ 1.47878742e+00, -8.31300139e-01,  1.13890839e+00],\n",
       "        [-1.37229991e+00, -1.01824069e+00,  2.46461701e+00],\n",
       "        [-7.72022605e-01, -7.80024290e-01,  9.61036921e-01],\n",
       "        [-1.81580639e+00, -1.72069120e+00,  7.81982183e-01],\n",
       "        [-8.14645112e-01,  2.73084372e-01,  2.57306874e-01],\n",
       "        [ 6.79837227e-01,  5.65692425e-01,  1.58233583e-01],\n",
       "        [-8.44228983e-01,  9.33009267e-01, -9.58902121e-01],\n",
       "        [-7.50647068e-01,  9.39831018e-01,  3.26543331e-01],\n",
       "        [-7.40796268e-01, -7.18987286e-01,  8.03421810e-02],\n",
       "        [ 5.32075405e-01, -5.13992965e-01, -3.05557162e-01],\n",
       "        [-1.37492001e-01, -6.47000849e-01, -1.10772789e+00],\n",
       "        [ 2.86451268e+00, -1.87413365e-01,  4.73086566e-01],\n",
       "        [ 1.01713133e+00, -1.57325983e+00, -9.70887959e-01],\n",
       "        [ 1.01333928e+00, -7.62525737e-01,  5.80954909e-01],\n",
       "        [-4.13364731e-02,  2.17657471e+00,  7.01908767e-01]],\n",
       "\n",
       "       [[ 1.77407968e+00, -7.76510239e-02,  8.15989316e-01],\n",
       "        [ 5.95721006e-01, -1.34556198e+00,  9.86428380e-01],\n",
       "        [ 6.95028901e-01, -7.49810755e-01,  6.89643979e-01],\n",
       "        [-5.10340035e-01,  1.02935827e+00,  3.39252889e-01],\n",
       "        [-1.00747883e+00,  1.12668902e-01,  3.09327275e-01],\n",
       "        [ 8.36588502e-01, -4.19202358e-01, -5.00131607e-01],\n",
       "        [-4.02565807e-01,  5.22649825e-01, -1.26395667e+00],\n",
       "        [ 3.85524854e-02,  9.32263792e-01, -2.91872770e-01],\n",
       "        [ 1.06024754e+00, -9.49351966e-01,  1.45965725e-01],\n",
       "        [-2.54456210e+00,  5.98095357e-01, -7.71900952e-01],\n",
       "        [ 1.19201951e-01, -1.51664662e+00, -2.18852475e-01],\n",
       "        [ 6.13353252e-01, -3.88842434e-01, -3.73007804e-01],\n",
       "        [ 1.02321219e+00,  5.45048237e-01,  1.14220440e+00],\n",
       "        [-8.24399590e-01,  6.36343583e-02, -2.33508237e-02],\n",
       "        [ 8.47078562e-01,  7.13855505e-01,  1.27986872e+00],\n",
       "        [-3.67277145e-01, -8.09128523e-01,  8.20278704e-01]],\n",
       "\n",
       "       [[-1.87440681e+00, -1.08894503e+00,  3.56561899e-01],\n",
       "        [-1.85334373e+00,  9.61262405e-01, -1.24864721e+00],\n",
       "        [-1.06292355e+00,  1.68521896e-01,  7.62786508e-01],\n",
       "        [ 1.54019976e+00,  2.17988062e+00,  2.81156254e+00],\n",
       "        [ 5.90399504e-01, -1.47856772e-01, -4.08498317e-01],\n",
       "        [ 1.39568162e+00,  2.90553540e-01, -9.11135912e-01],\n",
       "        [ 9.59002137e-01,  5.66565730e-02,  1.70940590e+00],\n",
       "        [ 3.24425310e-01, -7.28222728e-01,  4.56395805e-01],\n",
       "        [ 5.20967007e-01, -5.88827161e-03,  9.43952501e-01],\n",
       "        [ 6.70434892e-01,  1.53919590e+00,  1.32264268e+00],\n",
       "        [ 1.95462674e-01, -5.96239567e-01, -6.68712974e-01],\n",
       "        [-1.61136448e+00, -6.61814332e-01, -2.81334847e-01],\n",
       "        [ 6.43277824e-01, -5.62671244e-01, -3.47829491e-01],\n",
       "        [ 1.95966158e-02, -6.67764127e-01, -5.02785265e-01],\n",
       "        [ 9.70720798e-02,  1.15998805e+00, -6.79725707e-01],\n",
       "        [-9.37244892e-01,  9.21013951e-01,  4.82622385e-01]]],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.random.normal([4,32,32,3])\n",
    "# 行、列逆序间隔采样\n",
    "x[0,::-2,::-2] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=349, shape=(4, 32, 32), dtype=float32, numpy=\n",
       "array([[[-0.18618359,  1.6450189 , -0.6088316 , ...,  1.1786038 ,\n",
       "          0.6441293 , -0.0049975 ],\n",
       "        [ 0.06541731,  0.92101395,  1.217107  , ...,  0.9612624 ,\n",
       "          0.37216392, -1.088945  ],\n",
       "        [-0.44707197,  0.42131856,  0.6004422 , ..., -1.8124819 ,\n",
       "         -0.73415715,  1.9168302 ],\n",
       "        ...,\n",
       "        [-0.28756997, -1.2954223 , -1.632239  , ..., -0.9178357 ,\n",
       "         -1.5472794 , -0.21162711],\n",
       "        [ 1.1183071 ,  0.17118745, -0.99260473, ..., -1.4050789 ,\n",
       "         -1.230484  ,  0.09669129],\n",
       "        [ 0.6626053 ,  0.5269521 ,  1.2639095 , ..., -0.389043  ,\n",
       "          0.92543757,  1.0087351 ]],\n",
       "\n",
       "       [[-0.60660934,  0.24798833,  0.0728338 , ...,  0.5504535 ,\n",
       "         -0.9508979 , -0.17702754],\n",
       "        [ 0.39638186, -1.8665935 , -0.6956912 , ..., -1.1003441 ,\n",
       "         -1.2336079 , -0.11070947],\n",
       "        [-0.0738139 ,  0.66548616,  0.24967434, ...,  0.04689897,\n",
       "          2.3660183 ,  0.5228415 ],\n",
       "        ...,\n",
       "        [-0.9386713 ,  0.89552695, -0.06553846, ..., -2.06946   ,\n",
       "          0.16863483,  0.5518866 ],\n",
       "        [-0.13474576,  1.510267  ,  1.4518965 , ...,  0.510941  ,\n",
       "          1.3924757 , -0.21142066],\n",
       "        [ 1.0086203 , -0.6049397 ,  1.232256  , ..., -0.1353358 ,\n",
       "          0.17523175, -0.45147067]],\n",
       "\n",
       "       [[-0.06760669,  0.62925905,  0.43722135, ...,  0.91891634,\n",
       "          0.12730385,  0.41885048],\n",
       "        [-0.96445966, -0.10622662,  0.7457133 , ...,  1.59757   ,\n",
       "          0.9847417 , -1.4538193 ],\n",
       "        [-0.07818361,  1.1530604 , -0.04325892, ..., -1.5232185 ,\n",
       "         -0.02858032, -1.5687665 ],\n",
       "        ...,\n",
       "        [ 0.00266181,  2.6256945 ,  0.38865986, ...,  1.416888  ,\n",
       "         -0.08237544,  0.37160715],\n",
       "        [-0.7995192 , -0.09501592,  0.46073335, ..., -0.06711467,\n",
       "         -0.30661353,  1.3837487 ],\n",
       "        [-0.24280575,  1.5469192 , -0.9094142 , ..., -0.28770357,\n",
       "          0.49800137,  1.1714308 ]],\n",
       "\n",
       "       [[ 1.6170434 , -0.5348206 ,  1.2499245 , ..., -1.1716172 ,\n",
       "          0.35453954,  0.9994385 ],\n",
       "        [-0.33631572, -2.5866725 , -0.62708247, ...,  1.1061095 ,\n",
       "          1.8566949 , -0.4932627 ],\n",
       "        [-0.0135179 ,  0.17080994, -0.49821275, ..., -0.5687372 ,\n",
       "          0.15433708,  0.5196593 ],\n",
       "        ...,\n",
       "        [ 1.3374909 ,  0.7594115 ,  0.4481409 , ...,  0.73061645,\n",
       "          0.8989255 , -1.2772052 ],\n",
       "        [ 0.5839975 ,  1.539671  ,  0.89542776, ...,  0.61088943,\n",
       "         -1.1989498 ,  0.66583717],\n",
       "        [-0.6916377 , -0.3690044 ,  1.6008008 , ..., -0.57615155,\n",
       "          1.3824729 , -0.45877078]]], dtype=float32)>"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取 G 通道数据\n",
    "x[:,:,:,1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=353, shape=(2, 32, 32, 2), dtype=float32, numpy=\n",
       "array([[[[-0.18618359,  0.07031187],\n",
       "         [ 1.6450189 ,  0.348517  ],\n",
       "         [-0.6088316 , -0.621343  ],\n",
       "         ...,\n",
       "         [ 1.1786038 , -1.4287156 ],\n",
       "         [ 0.6441293 , -0.20440044],\n",
       "         [-0.0049975 ,  1.5488465 ]],\n",
       "\n",
       "        [[ 0.06541731, -0.84953684],\n",
       "         [ 0.92101395,  0.48262239],\n",
       "         [ 1.217107  , -1.4052657 ],\n",
       "         ...,\n",
       "         [ 0.9612624 , -1.2486472 ],\n",
       "         [ 0.37216392,  1.0750848 ],\n",
       "         [-1.088945  ,  0.3565619 ]],\n",
       "\n",
       "        [[-0.44707197,  2.167547  ],\n",
       "         [ 0.42131856,  0.50403583],\n",
       "         [ 0.6004422 ,  0.1319362 ],\n",
       "         ...,\n",
       "         [-1.8124819 ,  0.66180325],\n",
       "         [-0.73415715,  0.36450195],\n",
       "         [ 1.9168302 ,  0.7041983 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-0.28756997, -0.30589414],\n",
       "         [-1.2954223 , -0.5547997 ],\n",
       "         [-1.632239  ,  0.19882874],\n",
       "         ...,\n",
       "         [-0.9178357 ,  0.66117394],\n",
       "         [-1.5472794 , -0.00558463],\n",
       "         [-0.21162711,  0.5503712 ]],\n",
       "\n",
       "        [[ 1.1183071 , -0.6308083 ],\n",
       "         [ 0.17118745, -0.27917323],\n",
       "         [-0.99260473,  2.9608164 ],\n",
       "         ...,\n",
       "         [-1.4050789 ,  0.80981165],\n",
       "         [-1.230484  , -0.27027613],\n",
       "         [ 0.09669129, -2.6157568 ]],\n",
       "\n",
       "        [[ 0.6626053 , -1.1156315 ],\n",
       "         [ 0.5269521 , -0.25801212],\n",
       "         [ 1.2639095 ,  1.0886084 ],\n",
       "         ...,\n",
       "         [-0.389043  ,  1.7459022 ],\n",
       "         [ 0.92543757, -1.5551293 ],\n",
       "         [ 1.0087351 ,  1.3710026 ]]],\n",
       "\n",
       "\n",
       "       [[[-0.60660934, -1.2638452 ],\n",
       "         [ 0.24798833,  0.5227855 ],\n",
       "         [ 0.0728338 , -1.1823087 ],\n",
       "         ...,\n",
       "         [ 0.5504535 ,  0.40094876],\n",
       "         [-0.9508979 , -0.7456905 ],\n",
       "         [-0.17702754,  0.3559076 ]],\n",
       "\n",
       "        [[ 0.39638186, -1.7007005 ],\n",
       "         [-1.8665935 ,  1.1629176 ],\n",
       "         [-0.6956912 , -0.74835044],\n",
       "         ...,\n",
       "         [-1.1003441 , -0.44709206],\n",
       "         [-1.2336079 ,  1.256223  ],\n",
       "         [-0.11070947, -1.315627  ]],\n",
       "\n",
       "        [[-0.0738139 , -0.5373348 ],\n",
       "         [ 0.66548616,  1.1300471 ],\n",
       "         [ 0.24967434,  0.3936882 ],\n",
       "         ...,\n",
       "         [ 0.04689897,  0.98769116],\n",
       "         [ 2.3660183 ,  0.2919505 ],\n",
       "         [ 0.5228415 ,  1.1588289 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-0.9386713 ,  2.1835327 ],\n",
       "         [ 0.89552695, -0.2517788 ],\n",
       "         [-0.06553846,  1.4138005 ],\n",
       "         ...,\n",
       "         [-2.06946   , -1.0709397 ],\n",
       "         [ 0.16863483,  0.41027537],\n",
       "         [ 0.5518866 ,  0.97841364]],\n",
       "\n",
       "        [[-0.13474576,  0.5187225 ],\n",
       "         [ 1.510267  , -2.6055753 ],\n",
       "         [ 1.4518965 , -1.549036  ],\n",
       "         ...,\n",
       "         [ 0.510941  ,  1.419455  ],\n",
       "         [ 1.3924757 , -1.5852779 ],\n",
       "         [-0.21142066, -0.13225561]],\n",
       "\n",
       "        [[ 1.0086203 ,  0.48093295],\n",
       "         [-0.6049397 ,  1.5641924 ],\n",
       "         [ 1.232256  ,  1.4902121 ],\n",
       "         ...,\n",
       "         [-0.1353358 ,  0.24425314],\n",
       "         [ 0.17523175,  1.3854457 ],\n",
       "         [-0.45147067, -0.64985496]]]], dtype=float32)>"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取第 1~2 张图片的 G/B 通道数据\n",
    "# 高宽维度全部采集\n",
    "x[0:2,...,1:] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=357, shape=(2, 32, 32, 3), dtype=float32, numpy=\n",
       "array([[[[ 8.25723767e-01, -6.76066950e-02, -1.10346235e-01],\n",
       "         [-6.69988155e-01,  6.29259050e-01,  3.50434273e-01],\n",
       "         [-9.37441170e-01,  4.37221348e-01, -1.71617126e+00],\n",
       "         ...,\n",
       "         [-3.62230539e-01,  9.18916345e-01,  1.55754304e+00],\n",
       "         [ 1.02029324e+00,  1.27303854e-01,  9.84299541e-01],\n",
       "         [-6.16662085e-01,  4.18850482e-01, -3.01174849e-01]],\n",
       "\n",
       "        [[ 8.21057141e-01, -9.64459658e-01,  1.64778799e-01],\n",
       "         [-8.82190764e-01, -1.06226623e-01, -1.47489011e+00],\n",
       "         [-2.21317485e-01,  7.45713294e-01, -1.77864230e+00],\n",
       "         ...,\n",
       "         [-2.66044259e-01,  1.59756994e+00, -1.88045755e-01],\n",
       "         [ 5.00236928e-01,  9.84741688e-01,  1.50524330e+00],\n",
       "         [ 7.01813996e-02, -1.45381927e+00,  3.91711831e-01]],\n",
       "\n",
       "        [[-8.80369544e-01, -7.81836137e-02,  2.62227178e-01],\n",
       "         [-1.01223803e+00,  1.15306044e+00,  7.32809186e-01],\n",
       "         [-1.82157564e+00, -4.32589240e-02, -1.84647954e+00],\n",
       "         ...,\n",
       "         [ 2.67432421e-01, -1.52321851e+00,  2.24573463e-01],\n",
       "         [-7.63487101e-01, -2.85803154e-02, -2.90573150e-01],\n",
       "         [ 3.42371970e-01, -1.56876647e+00,  1.01103866e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 9.37411427e-01,  2.66180723e-03,  1.30410504e+00],\n",
       "         [ 5.77461243e-01,  2.62569451e+00, -1.67316151e+00],\n",
       "         [-1.59633219e+00,  3.88659865e-01, -1.65037334e-01],\n",
       "         ...,\n",
       "         [-1.42346346e+00,  1.41688800e+00,  4.60341543e-01],\n",
       "         [-5.69725513e-01, -8.23754370e-02, -8.93587768e-01],\n",
       "         [-7.09160507e-01,  3.71607155e-01,  7.28493810e-01]],\n",
       "\n",
       "        [[-1.04054010e+00, -7.99519181e-01, -1.85133827e+00],\n",
       "         [-2.26129293e+00, -9.50159207e-02,  1.02319050e+00],\n",
       "         [-7.76132584e-01,  4.60733354e-01,  2.26376438e+00],\n",
       "         ...,\n",
       "         [-9.72099245e-01, -6.71146736e-02,  1.16515625e+00],\n",
       "         [ 2.39086986e+00, -3.06613535e-01, -7.50397325e-01],\n",
       "         [-3.60201076e-02,  1.38374865e+00, -1.14711857e+00]],\n",
       "\n",
       "        [[ 1.21120059e+00, -2.42805749e-01,  4.32714611e-01],\n",
       "         [ 6.47391155e-02,  1.54691923e+00, -6.91374421e-01],\n",
       "         [ 1.45101500e+00, -9.09414172e-01, -1.37241542e-01],\n",
       "         ...,\n",
       "         [-9.78136837e-01, -2.87703574e-01, -2.01080471e-01],\n",
       "         [ 8.26935589e-01,  4.98001367e-01, -5.86189628e-01],\n",
       "         [-4.92031932e-01,  1.17143083e+00,  1.36817575e+00]]],\n",
       "\n",
       "\n",
       "       [[[-3.81229013e-01,  1.61704338e+00, -4.18111354e-01],\n",
       "         [ 9.15309966e-01, -5.34820616e-01,  1.19192910e+00],\n",
       "         [ 2.50840354e+00,  1.24992454e+00, -9.59224522e-01],\n",
       "         ...,\n",
       "         [ 6.99168503e-01, -1.17161715e+00,  3.12292171e+00],\n",
       "         [-2.13255100e-02,  3.54539543e-01, -1.58425078e-01],\n",
       "         [-1.55316186e+00,  9.99438524e-01, -5.86160362e-01]],\n",
       "\n",
       "        [[-2.10514665e+00, -3.36315721e-01, -2.57230788e-01],\n",
       "         [-6.23743594e-01, -2.58667254e+00,  9.45030808e-01],\n",
       "         [ 2.32226983e-01, -6.27082467e-01,  5.32616936e-02],\n",
       "         ...,\n",
       "         [-2.64275193e+00,  1.10610950e+00, -7.56104514e-02],\n",
       "         [ 1.84331203e+00,  1.85669494e+00, -6.76077902e-01],\n",
       "         [-1.50099129e-03, -4.93262708e-01,  1.11743379e+00]],\n",
       "\n",
       "        [[ 1.37985349e+00, -1.35178985e-02,  7.68749833e-01],\n",
       "         [ 5.73708296e-01,  1.70809940e-01, -6.59798920e-01],\n",
       "         [ 1.26097238e+00, -4.98212755e-01, -3.49319428e-01],\n",
       "         ...,\n",
       "         [-4.98646677e-01, -5.68737209e-01,  5.56033969e-01],\n",
       "         [-1.33648360e+00,  1.54337078e-01,  4.09233212e-01],\n",
       "         [-7.29186594e-01,  5.19659281e-01,  1.51651764e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 1.01125979e+00,  1.33749092e+00, -1.04760063e+00],\n",
       "         [-1.06510393e-01,  7.59411514e-01, -1.59222817e+00],\n",
       "         [ 3.16785201e-02,  4.48140889e-01,  8.77717078e-01],\n",
       "         ...,\n",
       "         [ 3.60435486e-01,  7.30616450e-01, -1.85181260e-01],\n",
       "         [ 8.15398060e-03,  8.98925483e-01, -1.36331961e-01],\n",
       "         [-7.09322214e-01, -1.27720523e+00,  1.45384097e+00]],\n",
       "\n",
       "        [[ 3.64372522e-01,  5.83997488e-01, -4.47822928e-01],\n",
       "         [ 6.00059986e-01,  1.53967094e+00, -1.03807771e+00],\n",
       "         [-6.30603433e-01,  8.95427763e-01,  9.04323637e-01],\n",
       "         ...,\n",
       "         [ 6.90305471e-01,  6.10889435e-01, -3.84706289e-01],\n",
       "         [ 4.93458539e-01, -1.19894981e+00,  9.20242488e-01],\n",
       "         [ 8.26259136e-01,  6.65837169e-01,  4.58709717e-01]],\n",
       "\n",
       "        [[-1.43966392e-01, -6.91637695e-01,  1.49261129e+00],\n",
       "         [-2.28133142e-01, -3.69004399e-01, -7.76805818e-01],\n",
       "         [ 6.46395683e-01,  1.60080075e+00, -2.29546881e+00],\n",
       "         ...,\n",
       "         [ 8.44625115e-01, -5.76151550e-01,  3.76763582e-01],\n",
       "         [ 1.53864956e+00,  1.38247287e+00, -1.05193186e+00],\n",
       "         [-7.07693920e-02, -4.58770782e-01,  5.47139831e-02]]]],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取最后 2 张图片\n",
    "# 高、宽、通道维度全部采集，等价于 x[2:]\n",
    "x[2:,...] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=361, shape=(4, 32, 32, 2), dtype=float32, numpy=\n",
       "array([[[[ 1.02277458e+00, -1.86183587e-01],\n",
       "         [-1.65232694e+00,  1.64501894e+00],\n",
       "         [ 9.07209754e-01, -6.08831584e-01],\n",
       "         ...,\n",
       "         [-1.49698746e+00,  1.17860377e+00],\n",
       "         [-7.12016821e-02,  6.44129276e-01],\n",
       "         [ 2.84651946e-02, -4.99749742e-03]],\n",
       "\n",
       "        [[ 1.30942535e+00,  6.54173121e-02],\n",
       "         [-9.37244892e-01,  9.21013951e-01],\n",
       "         [ 2.06201673e+00,  1.21710706e+00],\n",
       "         ...,\n",
       "         [-1.85334373e+00,  9.61262405e-01],\n",
       "         [ 2.95555860e-01,  3.72163922e-01],\n",
       "         [-1.87440681e+00, -1.08894503e+00]],\n",
       "\n",
       "        [[ 7.92588770e-01, -4.47071970e-01],\n",
       "         [ 2.03422457e-01,  4.21318561e-01],\n",
       "         [ 1.52484477e+00,  6.00442171e-01],\n",
       "         ...,\n",
       "         [-7.00447679e-01, -1.81248188e+00],\n",
       "         [ 1.41217303e+00, -7.34157145e-01],\n",
       "         [ 9.00036633e-01,  1.91683018e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 5.02298355e-01, -2.87569970e-01],\n",
       "         [ 2.39306951e+00, -1.29542232e+00],\n",
       "         [ 2.29123902e+00, -1.63223898e+00],\n",
       "         ...,\n",
       "         [-1.83718705e+00, -9.17835712e-01],\n",
       "         [ 2.15530306e-01, -1.54727936e+00],\n",
       "         [-3.25570285e-01, -2.11627111e-01]],\n",
       "\n",
       "        [[-1.42277992e+00,  1.11830711e+00],\n",
       "         [ 6.44002020e-01,  1.71187446e-01],\n",
       "         [ 1.34025335e-01, -9.92604733e-01],\n",
       "         ...,\n",
       "         [-2.67766982e-01, -1.40507889e+00],\n",
       "         [-7.80880392e-01, -1.23048401e+00],\n",
       "         [-1.61322927e+00,  9.66912881e-02]],\n",
       "\n",
       "        [[ 4.69642133e-01,  6.62605286e-01],\n",
       "         [ 1.47438809e-01,  5.26952088e-01],\n",
       "         [-9.95993257e-01,  1.26390946e+00],\n",
       "         ...,\n",
       "         [ 2.96741456e-01, -3.89043003e-01],\n",
       "         [-3.12943041e-01,  9.25437570e-01],\n",
       "         [-5.63943148e-01,  1.00873506e+00]]],\n",
       "\n",
       "\n",
       "       [[[-6.79350734e-01, -6.06609344e-01],\n",
       "         [-1.26136911e+00,  2.47988328e-01],\n",
       "         [ 5.07058859e-01,  7.28337988e-02],\n",
       "         ...,\n",
       "         [ 9.94867921e-01,  5.50453484e-01],\n",
       "         [ 1.42672563e+00, -9.50897872e-01],\n",
       "         [ 4.78412479e-01, -1.77027538e-01]],\n",
       "\n",
       "        [[-4.99683321e-01,  3.96381855e-01],\n",
       "         [-6.18508339e-01, -1.86659348e+00],\n",
       "         [ 6.64925754e-01, -6.95691228e-01],\n",
       "         ...,\n",
       "         [ 8.29323173e-01, -1.10034406e+00],\n",
       "         [ 4.18811440e-01, -1.23360789e+00],\n",
       "         [-4.33408976e-01, -1.10709466e-01]],\n",
       "\n",
       "        [[-6.15028143e-01, -7.38139004e-02],\n",
       "         [ 1.07878335e-01,  6.65486157e-01],\n",
       "         [-4.10441101e-01,  2.49674335e-01],\n",
       "         ...,\n",
       "         [ 9.67792511e-01,  4.68989685e-02],\n",
       "         [ 9.60961357e-02,  2.36601830e+00],\n",
       "         [-7.10562408e-01,  5.22841513e-01]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-3.92665267e-01, -9.38671291e-01],\n",
       "         [-2.04197121e+00,  8.95526946e-01],\n",
       "         [ 9.69459236e-01, -6.55384585e-02],\n",
       "         ...,\n",
       "         [-8.75938237e-01, -2.06945992e+00],\n",
       "         [-3.00159782e-01,  1.68634832e-01],\n",
       "         [ 3.00585806e-01,  5.51886618e-01]],\n",
       "\n",
       "        [[ 1.01840019e+00, -1.34745762e-01],\n",
       "         [ 1.24216855e+00,  1.51026702e+00],\n",
       "         [ 5.89153409e-01,  1.45189655e+00],\n",
       "         ...,\n",
       "         [-1.69628352e-01,  5.10941029e-01],\n",
       "         [-5.83331466e-01,  1.39247572e+00],\n",
       "         [-4.35953259e-01, -2.11420655e-01]],\n",
       "\n",
       "        [[-1.63264763e+00,  1.00862026e+00],\n",
       "         [-1.17007530e+00, -6.04939699e-01],\n",
       "         [-9.46718037e-01,  1.23225605e+00],\n",
       "         ...,\n",
       "         [-6.28432035e-02, -1.35335803e-01],\n",
       "         [ 3.74809086e-01,  1.75231755e-01],\n",
       "         [ 7.30279684e-01, -4.51470673e-01]]],\n",
       "\n",
       "\n",
       "       [[[ 8.25723767e-01, -6.76066950e-02],\n",
       "         [-6.69988155e-01,  6.29259050e-01],\n",
       "         [-9.37441170e-01,  4.37221348e-01],\n",
       "         ...,\n",
       "         [-3.62230539e-01,  9.18916345e-01],\n",
       "         [ 1.02029324e+00,  1.27303854e-01],\n",
       "         [-6.16662085e-01,  4.18850482e-01]],\n",
       "\n",
       "        [[ 8.21057141e-01, -9.64459658e-01],\n",
       "         [-8.82190764e-01, -1.06226623e-01],\n",
       "         [-2.21317485e-01,  7.45713294e-01],\n",
       "         ...,\n",
       "         [-2.66044259e-01,  1.59756994e+00],\n",
       "         [ 5.00236928e-01,  9.84741688e-01],\n",
       "         [ 7.01813996e-02, -1.45381927e+00]],\n",
       "\n",
       "        [[-8.80369544e-01, -7.81836137e-02],\n",
       "         [-1.01223803e+00,  1.15306044e+00],\n",
       "         [-1.82157564e+00, -4.32589240e-02],\n",
       "         ...,\n",
       "         [ 2.67432421e-01, -1.52321851e+00],\n",
       "         [-7.63487101e-01, -2.85803154e-02],\n",
       "         [ 3.42371970e-01, -1.56876647e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 9.37411427e-01,  2.66180723e-03],\n",
       "         [ 5.77461243e-01,  2.62569451e+00],\n",
       "         [-1.59633219e+00,  3.88659865e-01],\n",
       "         ...,\n",
       "         [-1.42346346e+00,  1.41688800e+00],\n",
       "         [-5.69725513e-01, -8.23754370e-02],\n",
       "         [-7.09160507e-01,  3.71607155e-01]],\n",
       "\n",
       "        [[-1.04054010e+00, -7.99519181e-01],\n",
       "         [-2.26129293e+00, -9.50159207e-02],\n",
       "         [-7.76132584e-01,  4.60733354e-01],\n",
       "         ...,\n",
       "         [-9.72099245e-01, -6.71146736e-02],\n",
       "         [ 2.39086986e+00, -3.06613535e-01],\n",
       "         [-3.60201076e-02,  1.38374865e+00]],\n",
       "\n",
       "        [[ 1.21120059e+00, -2.42805749e-01],\n",
       "         [ 6.47391155e-02,  1.54691923e+00],\n",
       "         [ 1.45101500e+00, -9.09414172e-01],\n",
       "         ...,\n",
       "         [-9.78136837e-01, -2.87703574e-01],\n",
       "         [ 8.26935589e-01,  4.98001367e-01],\n",
       "         [-4.92031932e-01,  1.17143083e+00]]],\n",
       "\n",
       "\n",
       "       [[[-3.81229013e-01,  1.61704338e+00],\n",
       "         [ 9.15309966e-01, -5.34820616e-01],\n",
       "         [ 2.50840354e+00,  1.24992454e+00],\n",
       "         ...,\n",
       "         [ 6.99168503e-01, -1.17161715e+00],\n",
       "         [-2.13255100e-02,  3.54539543e-01],\n",
       "         [-1.55316186e+00,  9.99438524e-01]],\n",
       "\n",
       "        [[-2.10514665e+00, -3.36315721e-01],\n",
       "         [-6.23743594e-01, -2.58667254e+00],\n",
       "         [ 2.32226983e-01, -6.27082467e-01],\n",
       "         ...,\n",
       "         [-2.64275193e+00,  1.10610950e+00],\n",
       "         [ 1.84331203e+00,  1.85669494e+00],\n",
       "         [-1.50099129e-03, -4.93262708e-01]],\n",
       "\n",
       "        [[ 1.37985349e+00, -1.35178985e-02],\n",
       "         [ 5.73708296e-01,  1.70809940e-01],\n",
       "         [ 1.26097238e+00, -4.98212755e-01],\n",
       "         ...,\n",
       "         [-4.98646677e-01, -5.68737209e-01],\n",
       "         [-1.33648360e+00,  1.54337078e-01],\n",
       "         [-7.29186594e-01,  5.19659281e-01]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 1.01125979e+00,  1.33749092e+00],\n",
       "         [-1.06510393e-01,  7.59411514e-01],\n",
       "         [ 3.16785201e-02,  4.48140889e-01],\n",
       "         ...,\n",
       "         [ 3.60435486e-01,  7.30616450e-01],\n",
       "         [ 8.15398060e-03,  8.98925483e-01],\n",
       "         [-7.09322214e-01, -1.27720523e+00]],\n",
       "\n",
       "        [[ 3.64372522e-01,  5.83997488e-01],\n",
       "         [ 6.00059986e-01,  1.53967094e+00],\n",
       "         [-6.30603433e-01,  8.95427763e-01],\n",
       "         ...,\n",
       "         [ 6.90305471e-01,  6.10889435e-01],\n",
       "         [ 4.93458539e-01, -1.19894981e+00],\n",
       "         [ 8.26259136e-01,  6.65837169e-01]],\n",
       "\n",
       "        [[-1.43966392e-01, -6.91637695e-01],\n",
       "         [-2.28133142e-01, -3.69004399e-01],\n",
       "         [ 6.46395683e-01,  1.60080075e+00],\n",
       "         ...,\n",
       "         [ 8.44625115e-01, -5.76151550e-01],\n",
       "         [ 1.53864956e+00,  1.38247287e+00],\n",
       "         [-7.07693920e-02, -4.58770782e-01]]]], dtype=float32)>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取 R/G 通道数据\n",
    "# 所有样本，所有高、宽的前 2 个通道\n",
    "x[...,:2] "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 维度变换\n",
    "\n",
    "### 改变视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=367, shape=(2, 4, 4, 3), dtype=int32, numpy=\n",
       "array([[[[ 0,  1,  2],\n",
       "         [ 3,  4,  5],\n",
       "         [ 6,  7,  8],\n",
       "         [ 9, 10, 11]],\n",
       "\n",
       "        [[12, 13, 14],\n",
       "         [15, 16, 17],\n",
       "         [18, 19, 20],\n",
       "         [21, 22, 23]],\n",
       "\n",
       "        [[24, 25, 26],\n",
       "         [27, 28, 29],\n",
       "         [30, 31, 32],\n",
       "         [33, 34, 35]],\n",
       "\n",
       "        [[36, 37, 38],\n",
       "         [39, 40, 41],\n",
       "         [42, 43, 44],\n",
       "         [45, 46, 47]]],\n",
       "\n",
       "\n",
       "       [[[48, 49, 50],\n",
       "         [51, 52, 53],\n",
       "         [54, 55, 56],\n",
       "         [57, 58, 59]],\n",
       "\n",
       "        [[60, 61, 62],\n",
       "         [63, 64, 65],\n",
       "         [66, 67, 68],\n",
       "         [69, 70, 71]],\n",
       "\n",
       "        [[72, 73, 74],\n",
       "         [75, 76, 77],\n",
       "         [78, 79, 80],\n",
       "         [81, 82, 83]],\n",
       "\n",
       "        [[84, 85, 86],\n",
       "         [87, 88, 89],\n",
       "         [90, 91, 92],\n",
       "         [93, 94, 95]]]])>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成向量\n",
    "x=tf.range(96)\n",
    "# 改变 x 的视图，获得 4D 张量，存储并未改变\n",
    "x=tf.reshape(x,[2,4,4,3]) \n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 改变视图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们通过 tf.range()模拟生成一个向量数据，并通过 tf.reshape 视图改变函数产生不同的视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=373, shape=(2, 4, 4, 3), dtype=int32, numpy=\n",
       "array([[[[ 0,  1,  2],\n",
       "         [ 3,  4,  5],\n",
       "         [ 6,  7,  8],\n",
       "         [ 9, 10, 11]],\n",
       "\n",
       "        [[12, 13, 14],\n",
       "         [15, 16, 17],\n",
       "         [18, 19, 20],\n",
       "         [21, 22, 23]],\n",
       "\n",
       "        [[24, 25, 26],\n",
       "         [27, 28, 29],\n",
       "         [30, 31, 32],\n",
       "         [33, 34, 35]],\n",
       "\n",
       "        [[36, 37, 38],\n",
       "         [39, 40, 41],\n",
       "         [42, 43, 44],\n",
       "         [45, 46, 47]]],\n",
       "\n",
       "\n",
       "       [[[48, 49, 50],\n",
       "         [51, 52, 53],\n",
       "         [54, 55, 56],\n",
       "         [57, 58, 59]],\n",
       "\n",
       "        [[60, 61, 62],\n",
       "         [63, 64, 65],\n",
       "         [66, 67, 68],\n",
       "         [69, 70, 71]],\n",
       "\n",
       "        [[72, 73, 74],\n",
       "         [75, 76, 77],\n",
       "         [78, 79, 80],\n",
       "         [81, 82, 83]],\n",
       "\n",
       "        [[84, 85, 86],\n",
       "         [87, 88, 89],\n",
       "         [90, 91, 92],\n",
       "         [93, 94, 95]]]])>"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成向量\n",
    "x = tf.range(96) \n",
    "# 改变 x 的视图，获得 4D 张量，存储并未改变\n",
    "x = tf.reshape(x,[2,4,4,3]) \n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, TensorShape([2, 4, 4, 3]))"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取张量的维度数和形状列表\n",
    "x.ndim,x.shape "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过 tf.reshape(x, new_shape)，可以将张量的视图任意地合法改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=375, shape=(2, 48), dtype=int32, numpy=\n",
       "array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,\n",
       "        16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,\n",
       "        32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],\n",
       "       [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\n",
       "        64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,\n",
       "        80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95]])>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.reshape(x,[2,-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=377, shape=(2, 4, 12), dtype=int32, numpy=\n",
       "array([[[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],\n",
       "        [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],\n",
       "        [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],\n",
       "        [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]],\n",
       "\n",
       "       [[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n",
       "        [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71],\n",
       "        [72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83],\n",
       "        [84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95]]])>"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " tf.reshape(x,[2,4,12])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=379, shape=(2, 16, 3), dtype=int32, numpy=\n",
       "array([[[ 0,  1,  2],\n",
       "        [ 3,  4,  5],\n",
       "        [ 6,  7,  8],\n",
       "        [ 9, 10, 11],\n",
       "        [12, 13, 14],\n",
       "        [15, 16, 17],\n",
       "        [18, 19, 20],\n",
       "        [21, 22, 23],\n",
       "        [24, 25, 26],\n",
       "        [27, 28, 29],\n",
       "        [30, 31, 32],\n",
       "        [33, 34, 35],\n",
       "        [36, 37, 38],\n",
       "        [39, 40, 41],\n",
       "        [42, 43, 44],\n",
       "        [45, 46, 47]],\n",
       "\n",
       "       [[48, 49, 50],\n",
       "        [51, 52, 53],\n",
       "        [54, 55, 56],\n",
       "        [57, 58, 59],\n",
       "        [60, 61, 62],\n",
       "        [63, 64, 65],\n",
       "        [66, 67, 68],\n",
       "        [69, 70, 71],\n",
       "        [72, 73, 74],\n",
       "        [75, 76, 77],\n",
       "        [78, 79, 80],\n",
       "        [81, 82, 83],\n",
       "        [84, 85, 86],\n",
       "        [87, 88, 89],\n",
       "        [90, 91, 92],\n",
       "        [93, 94, 95]]])>"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.reshape(x,[2,-1,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 增、删维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=383, shape=(28, 28), dtype=int32, numpy=\n",
       "array([[9, 6, 6, 9, 4, 2, 1, 1, 4, 1, 2, 0, 2, 6, 6, 7, 2, 1, 9, 7, 2, 8,\n",
       "        9, 0, 2, 0, 2, 9],\n",
       "       [9, 1, 0, 8, 8, 7, 3, 2, 4, 2, 8, 6, 4, 8, 9, 6, 1, 8, 2, 8, 2, 3,\n",
       "        8, 7, 6, 9, 2, 6],\n",
       "       [0, 8, 5, 7, 2, 7, 5, 5, 7, 1, 9, 8, 3, 6, 8, 8, 8, 1, 1, 3, 9, 3,\n",
       "        7, 2, 3, 4, 9, 3],\n",
       "       [3, 0, 6, 7, 4, 5, 0, 5, 3, 1, 8, 0, 7, 2, 5, 6, 5, 6, 4, 3, 8, 1,\n",
       "        0, 5, 1, 6, 8, 5],\n",
       "       [6, 4, 4, 6, 5, 1, 7, 9, 7, 8, 3, 4, 7, 9, 1, 3, 4, 7, 6, 8, 5, 4,\n",
       "        0, 8, 4, 7, 4, 4],\n",
       "       [9, 9, 0, 4, 7, 9, 9, 1, 9, 6, 3, 2, 0, 0, 6, 9, 2, 6, 3, 7, 4, 4,\n",
       "        2, 0, 0, 8, 2, 8],\n",
       "       [7, 0, 0, 7, 6, 7, 3, 6, 9, 8, 9, 0, 3, 1, 5, 4, 9, 2, 9, 2, 7, 8,\n",
       "        0, 2, 7, 4, 3, 9],\n",
       "       [3, 5, 6, 4, 9, 7, 3, 8, 3, 9, 1, 6, 2, 6, 3, 7, 2, 7, 0, 8, 2, 2,\n",
       "        4, 5, 3, 2, 7, 0],\n",
       "       [6, 3, 0, 2, 3, 2, 5, 8, 7, 2, 2, 6, 8, 6, 2, 0, 3, 6, 3, 6, 5, 6,\n",
       "        7, 6, 8, 5, 5, 1],\n",
       "       [5, 5, 1, 6, 3, 2, 7, 5, 6, 3, 7, 4, 2, 9, 3, 2, 1, 2, 8, 5, 6, 2,\n",
       "        3, 2, 7, 5, 4, 6],\n",
       "       [9, 2, 6, 3, 4, 3, 6, 9, 6, 2, 3, 7, 7, 9, 2, 3, 1, 2, 1, 8, 2, 0,\n",
       "        2, 3, 9, 2, 5, 4],\n",
       "       [2, 1, 3, 7, 7, 6, 7, 4, 4, 8, 3, 7, 9, 6, 9, 3, 2, 6, 9, 1, 1, 0,\n",
       "        6, 4, 5, 8, 4, 6],\n",
       "       [0, 7, 7, 2, 4, 7, 2, 6, 4, 4, 1, 1, 7, 0, 2, 6, 6, 0, 1, 3, 0, 2,\n",
       "        2, 6, 4, 9, 3, 1],\n",
       "       [3, 5, 0, 4, 0, 3, 5, 7, 3, 4, 3, 2, 3, 2, 4, 0, 7, 7, 7, 4, 4, 1,\n",
       "        2, 2, 4, 4, 2, 6],\n",
       "       [8, 0, 4, 1, 6, 7, 2, 0, 2, 8, 1, 5, 5, 1, 7, 0, 2, 6, 9, 3, 6, 4,\n",
       "        0, 9, 0, 5, 5, 7],\n",
       "       [5, 9, 4, 2, 3, 3, 6, 4, 7, 9, 4, 0, 5, 3, 3, 1, 9, 7, 6, 5, 5, 0,\n",
       "        9, 4, 4, 9, 5, 2],\n",
       "       [6, 7, 4, 4, 7, 3, 3, 3, 2, 7, 6, 6, 0, 8, 1, 7, 1, 8, 0, 9, 7, 3,\n",
       "        8, 9, 3, 5, 1, 4],\n",
       "       [5, 8, 4, 6, 7, 2, 8, 5, 7, 2, 1, 8, 2, 0, 4, 4, 3, 1, 8, 0, 9, 5,\n",
       "        5, 1, 0, 6, 9, 7],\n",
       "       [0, 1, 4, 9, 5, 3, 6, 8, 4, 7, 7, 4, 4, 3, 1, 8, 0, 0, 1, 4, 0, 8,\n",
       "        2, 8, 3, 4, 4, 9],\n",
       "       [0, 7, 5, 9, 9, 9, 3, 0, 9, 3, 6, 7, 4, 6, 9, 3, 0, 0, 6, 4, 8, 7,\n",
       "        7, 3, 4, 0, 5, 7],\n",
       "       [1, 6, 9, 1, 7, 5, 6, 9, 9, 2, 7, 6, 2, 3, 7, 9, 0, 1, 7, 1, 1, 0,\n",
       "        8, 5, 1, 1, 2, 7],\n",
       "       [0, 0, 9, 7, 8, 2, 6, 8, 0, 6, 3, 9, 5, 2, 5, 4, 3, 4, 8, 6, 2, 6,\n",
       "        8, 5, 1, 6, 2, 2],\n",
       "       [9, 1, 3, 5, 2, 3, 9, 2, 2, 9, 1, 1, 3, 5, 1, 3, 0, 8, 0, 1, 8, 2,\n",
       "        9, 5, 6, 9, 8, 3],\n",
       "       [7, 2, 5, 8, 2, 9, 4, 8, 1, 5, 5, 9, 4, 4, 1, 1, 1, 9, 0, 0, 1, 8,\n",
       "        0, 2, 7, 9, 8, 6],\n",
       "       [7, 3, 3, 8, 1, 5, 9, 4, 8, 0, 4, 4, 5, 1, 2, 2, 4, 8, 5, 8, 4, 6,\n",
       "        8, 6, 0, 0, 5, 2],\n",
       "       [2, 0, 3, 7, 4, 3, 2, 4, 7, 5, 8, 6, 6, 5, 8, 2, 3, 7, 9, 0, 6, 4,\n",
       "        6, 5, 8, 8, 6, 0],\n",
       "       [4, 1, 4, 1, 2, 8, 0, 1, 2, 7, 8, 0, 5, 8, 1, 0, 0, 2, 7, 0, 1, 3,\n",
       "        7, 4, 6, 1, 0, 7],\n",
       "       [6, 8, 3, 2, 1, 4, 9, 3, 7, 8, 9, 3, 5, 0, 2, 3, 7, 2, 0, 1, 6, 5,\n",
       "        0, 6, 7, 8, 1, 4]])>"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 产生矩阵\n",
    "x = tf.random.uniform([28,28],maxval=10,dtype=tf.int32)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过 tf.expand_dims(x, axis)可在指定的 axis 轴前可以插入一个新的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=385, shape=(28, 28, 1), dtype=int32, numpy=\n",
       "array([[[9],\n",
       "        [6],\n",
       "        [6],\n",
       "        [9],\n",
       "        [4],\n",
       "        [2],\n",
       "        [1],\n",
       "        [1],\n",
       "        [4],\n",
       "        [1],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [6],\n",
       "        [6],\n",
       "        [7],\n",
       "        [2],\n",
       "        [1],\n",
       "        [9],\n",
       "        [7],\n",
       "        [2],\n",
       "        [8],\n",
       "        [9],\n",
       "        [0],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [9]],\n",
       "\n",
       "       [[9],\n",
       "        [1],\n",
       "        [0],\n",
       "        [8],\n",
       "        [8],\n",
       "        [7],\n",
       "        [3],\n",
       "        [2],\n",
       "        [4],\n",
       "        [2],\n",
       "        [8],\n",
       "        [6],\n",
       "        [4],\n",
       "        [8],\n",
       "        [9],\n",
       "        [6],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [8],\n",
       "        [2],\n",
       "        [3],\n",
       "        [8],\n",
       "        [7],\n",
       "        [6],\n",
       "        [9],\n",
       "        [2],\n",
       "        [6]],\n",
       "\n",
       "       [[0],\n",
       "        [8],\n",
       "        [5],\n",
       "        [7],\n",
       "        [2],\n",
       "        [7],\n",
       "        [5],\n",
       "        [5],\n",
       "        [7],\n",
       "        [1],\n",
       "        [9],\n",
       "        [8],\n",
       "        [3],\n",
       "        [6],\n",
       "        [8],\n",
       "        [8],\n",
       "        [8],\n",
       "        [1],\n",
       "        [1],\n",
       "        [3],\n",
       "        [9],\n",
       "        [3],\n",
       "        [7],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4],\n",
       "        [9],\n",
       "        [3]],\n",
       "\n",
       "       [[3],\n",
       "        [0],\n",
       "        [6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [5],\n",
       "        [0],\n",
       "        [5],\n",
       "        [3],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [7],\n",
       "        [2],\n",
       "        [5],\n",
       "        [6],\n",
       "        [5],\n",
       "        [6],\n",
       "        [4],\n",
       "        [3],\n",
       "        [8],\n",
       "        [1],\n",
       "        [0],\n",
       "        [5],\n",
       "        [1],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5]],\n",
       "\n",
       "       [[6],\n",
       "        [4],\n",
       "        [4],\n",
       "        [6],\n",
       "        [5],\n",
       "        [1],\n",
       "        [7],\n",
       "        [9],\n",
       "        [7],\n",
       "        [8],\n",
       "        [3],\n",
       "        [4],\n",
       "        [7],\n",
       "        [9],\n",
       "        [1],\n",
       "        [3],\n",
       "        [4],\n",
       "        [7],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5],\n",
       "        [4],\n",
       "        [0],\n",
       "        [8],\n",
       "        [4],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4]],\n",
       "\n",
       "       [[9],\n",
       "        [9],\n",
       "        [0],\n",
       "        [4],\n",
       "        [7],\n",
       "        [9],\n",
       "        [9],\n",
       "        [1],\n",
       "        [9],\n",
       "        [6],\n",
       "        [3],\n",
       "        [2],\n",
       "        [0],\n",
       "        [0],\n",
       "        [6],\n",
       "        [9],\n",
       "        [2],\n",
       "        [6],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [2],\n",
       "        [0],\n",
       "        [0],\n",
       "        [8],\n",
       "        [2],\n",
       "        [8]],\n",
       "\n",
       "       [[7],\n",
       "        [0],\n",
       "        [0],\n",
       "        [7],\n",
       "        [6],\n",
       "        [7],\n",
       "        [3],\n",
       "        [6],\n",
       "        [9],\n",
       "        [8],\n",
       "        [9],\n",
       "        [0],\n",
       "        [3],\n",
       "        [1],\n",
       "        [5],\n",
       "        [4],\n",
       "        [9],\n",
       "        [2],\n",
       "        [9],\n",
       "        [2],\n",
       "        [7],\n",
       "        [8],\n",
       "        [0],\n",
       "        [2],\n",
       "        [7],\n",
       "        [4],\n",
       "        [3],\n",
       "        [9]],\n",
       "\n",
       "       [[3],\n",
       "        [5],\n",
       "        [6],\n",
       "        [4],\n",
       "        [9],\n",
       "        [7],\n",
       "        [3],\n",
       "        [8],\n",
       "        [3],\n",
       "        [9],\n",
       "        [1],\n",
       "        [6],\n",
       "        [2],\n",
       "        [6],\n",
       "        [3],\n",
       "        [7],\n",
       "        [2],\n",
       "        [7],\n",
       "        [0],\n",
       "        [8],\n",
       "        [2],\n",
       "        [2],\n",
       "        [4],\n",
       "        [5],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [0]],\n",
       "\n",
       "       [[6],\n",
       "        [3],\n",
       "        [0],\n",
       "        [2],\n",
       "        [3],\n",
       "        [2],\n",
       "        [5],\n",
       "        [8],\n",
       "        [7],\n",
       "        [2],\n",
       "        [2],\n",
       "        [6],\n",
       "        [8],\n",
       "        [6],\n",
       "        [2],\n",
       "        [0],\n",
       "        [3],\n",
       "        [6],\n",
       "        [3],\n",
       "        [6],\n",
       "        [5],\n",
       "        [6],\n",
       "        [7],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5],\n",
       "        [5],\n",
       "        [1]],\n",
       "\n",
       "       [[5],\n",
       "        [5],\n",
       "        [1],\n",
       "        [6],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [5],\n",
       "        [6],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [2],\n",
       "        [9],\n",
       "        [3],\n",
       "        [2],\n",
       "        [1],\n",
       "        [2],\n",
       "        [8],\n",
       "        [5],\n",
       "        [6],\n",
       "        [2],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [5],\n",
       "        [4],\n",
       "        [6]],\n",
       "\n",
       "       [[9],\n",
       "        [2],\n",
       "        [6],\n",
       "        [3],\n",
       "        [4],\n",
       "        [3],\n",
       "        [6],\n",
       "        [9],\n",
       "        [6],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [7],\n",
       "        [9],\n",
       "        [2],\n",
       "        [3],\n",
       "        [1],\n",
       "        [2],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [3],\n",
       "        [9],\n",
       "        [2],\n",
       "        [5],\n",
       "        [4]],\n",
       "\n",
       "       [[2],\n",
       "        [1],\n",
       "        [3],\n",
       "        [7],\n",
       "        [7],\n",
       "        [6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [8],\n",
       "        [3],\n",
       "        [7],\n",
       "        [9],\n",
       "        [6],\n",
       "        [9],\n",
       "        [3],\n",
       "        [2],\n",
       "        [6],\n",
       "        [9],\n",
       "        [1],\n",
       "        [1],\n",
       "        [0],\n",
       "        [6],\n",
       "        [4],\n",
       "        [5],\n",
       "        [8],\n",
       "        [4],\n",
       "        [6]],\n",
       "\n",
       "       [[0],\n",
       "        [7],\n",
       "        [7],\n",
       "        [2],\n",
       "        [4],\n",
       "        [7],\n",
       "        [2],\n",
       "        [6],\n",
       "        [4],\n",
       "        [4],\n",
       "        [1],\n",
       "        [1],\n",
       "        [7],\n",
       "        [0],\n",
       "        [2],\n",
       "        [6],\n",
       "        [6],\n",
       "        [0],\n",
       "        [1],\n",
       "        [3],\n",
       "        [0],\n",
       "        [2],\n",
       "        [2],\n",
       "        [6],\n",
       "        [4],\n",
       "        [9],\n",
       "        [3],\n",
       "        [1]],\n",
       "\n",
       "       [[3],\n",
       "        [5],\n",
       "        [0],\n",
       "        [4],\n",
       "        [0],\n",
       "        [3],\n",
       "        [5],\n",
       "        [7],\n",
       "        [3],\n",
       "        [4],\n",
       "        [3],\n",
       "        [2],\n",
       "        [3],\n",
       "        [2],\n",
       "        [4],\n",
       "        [0],\n",
       "        [7],\n",
       "        [7],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [1],\n",
       "        [2],\n",
       "        [2],\n",
       "        [4],\n",
       "        [4],\n",
       "        [2],\n",
       "        [6]],\n",
       "\n",
       "       [[8],\n",
       "        [0],\n",
       "        [4],\n",
       "        [1],\n",
       "        [6],\n",
       "        [7],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [8],\n",
       "        [1],\n",
       "        [5],\n",
       "        [5],\n",
       "        [1],\n",
       "        [7],\n",
       "        [0],\n",
       "        [2],\n",
       "        [6],\n",
       "        [9],\n",
       "        [3],\n",
       "        [6],\n",
       "        [4],\n",
       "        [0],\n",
       "        [9],\n",
       "        [0],\n",
       "        [5],\n",
       "        [5],\n",
       "        [7]],\n",
       "\n",
       "       [[5],\n",
       "        [9],\n",
       "        [4],\n",
       "        [2],\n",
       "        [3],\n",
       "        [3],\n",
       "        [6],\n",
       "        [4],\n",
       "        [7],\n",
       "        [9],\n",
       "        [4],\n",
       "        [0],\n",
       "        [5],\n",
       "        [3],\n",
       "        [3],\n",
       "        [1],\n",
       "        [9],\n",
       "        [7],\n",
       "        [6],\n",
       "        [5],\n",
       "        [5],\n",
       "        [0],\n",
       "        [9],\n",
       "        [4],\n",
       "        [4],\n",
       "        [9],\n",
       "        [5],\n",
       "        [2]],\n",
       "\n",
       "       [[6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [7],\n",
       "        [3],\n",
       "        [3],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [6],\n",
       "        [6],\n",
       "        [0],\n",
       "        [8],\n",
       "        [1],\n",
       "        [7],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [9],\n",
       "        [7],\n",
       "        [3],\n",
       "        [8],\n",
       "        [9],\n",
       "        [3],\n",
       "        [5],\n",
       "        [1],\n",
       "        [4]],\n",
       "\n",
       "       [[5],\n",
       "        [8],\n",
       "        [4],\n",
       "        [6],\n",
       "        [7],\n",
       "        [2],\n",
       "        [8],\n",
       "        [5],\n",
       "        [7],\n",
       "        [2],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [0],\n",
       "        [4],\n",
       "        [4],\n",
       "        [3],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [9],\n",
       "        [5],\n",
       "        [5],\n",
       "        [1],\n",
       "        [0],\n",
       "        [6],\n",
       "        [9],\n",
       "        [7]],\n",
       "\n",
       "       [[0],\n",
       "        [1],\n",
       "        [4],\n",
       "        [9],\n",
       "        [5],\n",
       "        [3],\n",
       "        [6],\n",
       "        [8],\n",
       "        [4],\n",
       "        [7],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [3],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [0],\n",
       "        [1],\n",
       "        [4],\n",
       "        [0],\n",
       "        [8],\n",
       "        [2],\n",
       "        [8],\n",
       "        [3],\n",
       "        [4],\n",
       "        [4],\n",
       "        [9]],\n",
       "\n",
       "       [[0],\n",
       "        [7],\n",
       "        [5],\n",
       "        [9],\n",
       "        [9],\n",
       "        [9],\n",
       "        [3],\n",
       "        [0],\n",
       "        [9],\n",
       "        [3],\n",
       "        [6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [6],\n",
       "        [9],\n",
       "        [3],\n",
       "        [0],\n",
       "        [0],\n",
       "        [6],\n",
       "        [4],\n",
       "        [8],\n",
       "        [7],\n",
       "        [7],\n",
       "        [3],\n",
       "        [4],\n",
       "        [0],\n",
       "        [5],\n",
       "        [7]],\n",
       "\n",
       "       [[1],\n",
       "        [6],\n",
       "        [9],\n",
       "        [1],\n",
       "        [7],\n",
       "        [5],\n",
       "        [6],\n",
       "        [9],\n",
       "        [9],\n",
       "        [2],\n",
       "        [7],\n",
       "        [6],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [9],\n",
       "        [0],\n",
       "        [1],\n",
       "        [7],\n",
       "        [1],\n",
       "        [1],\n",
       "        [0],\n",
       "        [8],\n",
       "        [5],\n",
       "        [1],\n",
       "        [1],\n",
       "        [2],\n",
       "        [7]],\n",
       "\n",
       "       [[0],\n",
       "        [0],\n",
       "        [9],\n",
       "        [7],\n",
       "        [8],\n",
       "        [2],\n",
       "        [6],\n",
       "        [8],\n",
       "        [0],\n",
       "        [6],\n",
       "        [3],\n",
       "        [9],\n",
       "        [5],\n",
       "        [2],\n",
       "        [5],\n",
       "        [4],\n",
       "        [3],\n",
       "        [4],\n",
       "        [8],\n",
       "        [6],\n",
       "        [2],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5],\n",
       "        [1],\n",
       "        [6],\n",
       "        [2],\n",
       "        [2]],\n",
       "\n",
       "       [[9],\n",
       "        [1],\n",
       "        [3],\n",
       "        [5],\n",
       "        [2],\n",
       "        [3],\n",
       "        [9],\n",
       "        [2],\n",
       "        [2],\n",
       "        [9],\n",
       "        [1],\n",
       "        [1],\n",
       "        [3],\n",
       "        [5],\n",
       "        [1],\n",
       "        [3],\n",
       "        [0],\n",
       "        [8],\n",
       "        [0],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [9],\n",
       "        [5],\n",
       "        [6],\n",
       "        [9],\n",
       "        [8],\n",
       "        [3]],\n",
       "\n",
       "       [[7],\n",
       "        [2],\n",
       "        [5],\n",
       "        [8],\n",
       "        [2],\n",
       "        [9],\n",
       "        [4],\n",
       "        [8],\n",
       "        [1],\n",
       "        [5],\n",
       "        [5],\n",
       "        [9],\n",
       "        [4],\n",
       "        [4],\n",
       "        [1],\n",
       "        [1],\n",
       "        [1],\n",
       "        [9],\n",
       "        [0],\n",
       "        [0],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [2],\n",
       "        [7],\n",
       "        [9],\n",
       "        [8],\n",
       "        [6]],\n",
       "\n",
       "       [[7],\n",
       "        [3],\n",
       "        [3],\n",
       "        [8],\n",
       "        [1],\n",
       "        [5],\n",
       "        [9],\n",
       "        [4],\n",
       "        [8],\n",
       "        [0],\n",
       "        [4],\n",
       "        [4],\n",
       "        [5],\n",
       "        [1],\n",
       "        [2],\n",
       "        [2],\n",
       "        [4],\n",
       "        [8],\n",
       "        [5],\n",
       "        [8],\n",
       "        [4],\n",
       "        [6],\n",
       "        [8],\n",
       "        [6],\n",
       "        [0],\n",
       "        [0],\n",
       "        [5],\n",
       "        [2]],\n",
       "\n",
       "       [[2],\n",
       "        [0],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [3],\n",
       "        [2],\n",
       "        [4],\n",
       "        [7],\n",
       "        [5],\n",
       "        [8],\n",
       "        [6],\n",
       "        [6],\n",
       "        [5],\n",
       "        [8],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [9],\n",
       "        [0],\n",
       "        [6],\n",
       "        [4],\n",
       "        [6],\n",
       "        [5],\n",
       "        [8],\n",
       "        [8],\n",
       "        [6],\n",
       "        [0]],\n",
       "\n",
       "       [[4],\n",
       "        [1],\n",
       "        [4],\n",
       "        [1],\n",
       "        [2],\n",
       "        [8],\n",
       "        [0],\n",
       "        [1],\n",
       "        [2],\n",
       "        [7],\n",
       "        [8],\n",
       "        [0],\n",
       "        [5],\n",
       "        [8],\n",
       "        [1],\n",
       "        [0],\n",
       "        [0],\n",
       "        [2],\n",
       "        [7],\n",
       "        [0],\n",
       "        [1],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [6],\n",
       "        [1],\n",
       "        [0],\n",
       "        [7]],\n",
       "\n",
       "       [[6],\n",
       "        [8],\n",
       "        [3],\n",
       "        [2],\n",
       "        [1],\n",
       "        [4],\n",
       "        [9],\n",
       "        [3],\n",
       "        [7],\n",
       "        [8],\n",
       "        [9],\n",
       "        [3],\n",
       "        [5],\n",
       "        [0],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [2],\n",
       "        [0],\n",
       "        [1],\n",
       "        [6],\n",
       "        [5],\n",
       "        [0],\n",
       "        [6],\n",
       "        [7],\n",
       "        [8],\n",
       "        [1],\n",
       "        [4]]])>"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# axis=2 表示宽维度后面的一个维度\n",
    "x = tf.expand_dims(x,axis=2) \n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=387, shape=(1, 28, 28, 1), dtype=int32, numpy=\n",
       "array([[[[9],\n",
       "         [6],\n",
       "         [6],\n",
       "         [9],\n",
       "         [4],\n",
       "         [2],\n",
       "         [1],\n",
       "         [1],\n",
       "         [4],\n",
       "         [1],\n",
       "         [2],\n",
       "         [0],\n",
       "         [2],\n",
       "         [6],\n",
       "         [6],\n",
       "         [7],\n",
       "         [2],\n",
       "         [1],\n",
       "         [9],\n",
       "         [7],\n",
       "         [2],\n",
       "         [8],\n",
       "         [9],\n",
       "         [0],\n",
       "         [2],\n",
       "         [0],\n",
       "         [2],\n",
       "         [9]],\n",
       "\n",
       "        [[9],\n",
       "         [1],\n",
       "         [0],\n",
       "         [8],\n",
       "         [8],\n",
       "         [7],\n",
       "         [3],\n",
       "         [2],\n",
       "         [4],\n",
       "         [2],\n",
       "         [8],\n",
       "         [6],\n",
       "         [4],\n",
       "         [8],\n",
       "         [9],\n",
       "         [6],\n",
       "         [1],\n",
       "         [8],\n",
       "         [2],\n",
       "         [8],\n",
       "         [2],\n",
       "         [3],\n",
       "         [8],\n",
       "         [7],\n",
       "         [6],\n",
       "         [9],\n",
       "         [2],\n",
       "         [6]],\n",
       "\n",
       "        [[0],\n",
       "         [8],\n",
       "         [5],\n",
       "         [7],\n",
       "         [2],\n",
       "         [7],\n",
       "         [5],\n",
       "         [5],\n",
       "         [7],\n",
       "         [1],\n",
       "         [9],\n",
       "         [8],\n",
       "         [3],\n",
       "         [6],\n",
       "         [8],\n",
       "         [8],\n",
       "         [8],\n",
       "         [1],\n",
       "         [1],\n",
       "         [3],\n",
       "         [9],\n",
       "         [3],\n",
       "         [7],\n",
       "         [2],\n",
       "         [3],\n",
       "         [4],\n",
       "         [9],\n",
       "         [3]],\n",
       "\n",
       "        [[3],\n",
       "         [0],\n",
       "         [6],\n",
       "         [7],\n",
       "         [4],\n",
       "         [5],\n",
       "         [0],\n",
       "         [5],\n",
       "         [3],\n",
       "         [1],\n",
       "         [8],\n",
       "         [0],\n",
       "         [7],\n",
       "         [2],\n",
       "         [5],\n",
       "         [6],\n",
       "         [5],\n",
       "         [6],\n",
       "         [4],\n",
       "         [3],\n",
       "         [8],\n",
       "         [1],\n",
       "         [0],\n",
       "         [5],\n",
       "         [1],\n",
       "         [6],\n",
       "         [8],\n",
       "         [5]],\n",
       "\n",
       "        [[6],\n",
       "         [4],\n",
       "         [4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [1],\n",
       "         [7],\n",
       "         [9],\n",
       "         [7],\n",
       "         [8],\n",
       "         [3],\n",
       "         [4],\n",
       "         [7],\n",
       "         [9],\n",
       "         [1],\n",
       "         [3],\n",
       "         [4],\n",
       "         [7],\n",
       "         [6],\n",
       "         [8],\n",
       "         [5],\n",
       "         [4],\n",
       "         [0],\n",
       "         [8],\n",
       "         [4],\n",
       "         [7],\n",
       "         [4],\n",
       "         [4]],\n",
       "\n",
       "        [[9],\n",
       "         [9],\n",
       "         [0],\n",
       "         [4],\n",
       "         [7],\n",
       "         [9],\n",
       "         [9],\n",
       "         [1],\n",
       "         [9],\n",
       "         [6],\n",
       "         [3],\n",
       "         [2],\n",
       "         [0],\n",
       "         [0],\n",
       "         [6],\n",
       "         [9],\n",
       "         [2],\n",
       "         [6],\n",
       "         [3],\n",
       "         [7],\n",
       "         [4],\n",
       "         [4],\n",
       "         [2],\n",
       "         [0],\n",
       "         [0],\n",
       "         [8],\n",
       "         [2],\n",
       "         [8]],\n",
       "\n",
       "        [[7],\n",
       "         [0],\n",
       "         [0],\n",
       "         [7],\n",
       "         [6],\n",
       "         [7],\n",
       "         [3],\n",
       "         [6],\n",
       "         [9],\n",
       "         [8],\n",
       "         [9],\n",
       "         [0],\n",
       "         [3],\n",
       "         [1],\n",
       "         [5],\n",
       "         [4],\n",
       "         [9],\n",
       "         [2],\n",
       "         [9],\n",
       "         [2],\n",
       "         [7],\n",
       "         [8],\n",
       "         [0],\n",
       "         [2],\n",
       "         [7],\n",
       "         [4],\n",
       "         [3],\n",
       "         [9]],\n",
       "\n",
       "        [[3],\n",
       "         [5],\n",
       "         [6],\n",
       "         [4],\n",
       "         [9],\n",
       "         [7],\n",
       "         [3],\n",
       "         [8],\n",
       "         [3],\n",
       "         [9],\n",
       "         [1],\n",
       "         [6],\n",
       "         [2],\n",
       "         [6],\n",
       "         [3],\n",
       "         [7],\n",
       "         [2],\n",
       "         [7],\n",
       "         [0],\n",
       "         [8],\n",
       "         [2],\n",
       "         [2],\n",
       "         [4],\n",
       "         [5],\n",
       "         [3],\n",
       "         [2],\n",
       "         [7],\n",
       "         [0]],\n",
       "\n",
       "        [[6],\n",
       "         [3],\n",
       "         [0],\n",
       "         [2],\n",
       "         [3],\n",
       "         [2],\n",
       "         [5],\n",
       "         [8],\n",
       "         [7],\n",
       "         [2],\n",
       "         [2],\n",
       "         [6],\n",
       "         [8],\n",
       "         [6],\n",
       "         [2],\n",
       "         [0],\n",
       "         [3],\n",
       "         [6],\n",
       "         [3],\n",
       "         [6],\n",
       "         [5],\n",
       "         [6],\n",
       "         [7],\n",
       "         [6],\n",
       "         [8],\n",
       "         [5],\n",
       "         [5],\n",
       "         [1]],\n",
       "\n",
       "        [[5],\n",
       "         [5],\n",
       "         [1],\n",
       "         [6],\n",
       "         [3],\n",
       "         [2],\n",
       "         [7],\n",
       "         [5],\n",
       "         [6],\n",
       "         [3],\n",
       "         [7],\n",
       "         [4],\n",
       "         [2],\n",
       "         [9],\n",
       "         [3],\n",
       "         [2],\n",
       "         [1],\n",
       "         [2],\n",
       "         [8],\n",
       "         [5],\n",
       "         [6],\n",
       "         [2],\n",
       "         [3],\n",
       "         [2],\n",
       "         [7],\n",
       "         [5],\n",
       "         [4],\n",
       "         [6]],\n",
       "\n",
       "        [[9],\n",
       "         [2],\n",
       "         [6],\n",
       "         [3],\n",
       "         [4],\n",
       "         [3],\n",
       "         [6],\n",
       "         [9],\n",
       "         [6],\n",
       "         [2],\n",
       "         [3],\n",
       "         [7],\n",
       "         [7],\n",
       "         [9],\n",
       "         [2],\n",
       "         [3],\n",
       "         [1],\n",
       "         [2],\n",
       "         [1],\n",
       "         [8],\n",
       "         [2],\n",
       "         [0],\n",
       "         [2],\n",
       "         [3],\n",
       "         [9],\n",
       "         [2],\n",
       "         [5],\n",
       "         [4]],\n",
       "\n",
       "        [[2],\n",
       "         [1],\n",
       "         [3],\n",
       "         [7],\n",
       "         [7],\n",
       "         [6],\n",
       "         [7],\n",
       "         [4],\n",
       "         [4],\n",
       "         [8],\n",
       "         [3],\n",
       "         [7],\n",
       "         [9],\n",
       "         [6],\n",
       "         [9],\n",
       "         [3],\n",
       "         [2],\n",
       "         [6],\n",
       "         [9],\n",
       "         [1],\n",
       "         [1],\n",
       "         [0],\n",
       "         [6],\n",
       "         [4],\n",
       "         [5],\n",
       "         [8],\n",
       "         [4],\n",
       "         [6]],\n",
       "\n",
       "        [[0],\n",
       "         [7],\n",
       "         [7],\n",
       "         [2],\n",
       "         [4],\n",
       "         [7],\n",
       "         [2],\n",
       "         [6],\n",
       "         [4],\n",
       "         [4],\n",
       "         [1],\n",
       "         [1],\n",
       "         [7],\n",
       "         [0],\n",
       "         [2],\n",
       "         [6],\n",
       "         [6],\n",
       "         [0],\n",
       "         [1],\n",
       "         [3],\n",
       "         [0],\n",
       "         [2],\n",
       "         [2],\n",
       "         [6],\n",
       "         [4],\n",
       "         [9],\n",
       "         [3],\n",
       "         [1]],\n",
       "\n",
       "        [[3],\n",
       "         [5],\n",
       "         [0],\n",
       "         [4],\n",
       "         [0],\n",
       "         [3],\n",
       "         [5],\n",
       "         [7],\n",
       "         [3],\n",
       "         [4],\n",
       "         [3],\n",
       "         [2],\n",
       "         [3],\n",
       "         [2],\n",
       "         [4],\n",
       "         [0],\n",
       "         [7],\n",
       "         [7],\n",
       "         [7],\n",
       "         [4],\n",
       "         [4],\n",
       "         [1],\n",
       "         [2],\n",
       "         [2],\n",
       "         [4],\n",
       "         [4],\n",
       "         [2],\n",
       "         [6]],\n",
       "\n",
       "        [[8],\n",
       "         [0],\n",
       "         [4],\n",
       "         [1],\n",
       "         [6],\n",
       "         [7],\n",
       "         [2],\n",
       "         [0],\n",
       "         [2],\n",
       "         [8],\n",
       "         [1],\n",
       "         [5],\n",
       "         [5],\n",
       "         [1],\n",
       "         [7],\n",
       "         [0],\n",
       "         [2],\n",
       "         [6],\n",
       "         [9],\n",
       "         [3],\n",
       "         [6],\n",
       "         [4],\n",
       "         [0],\n",
       "         [9],\n",
       "         [0],\n",
       "         [5],\n",
       "         [5],\n",
       "         [7]],\n",
       "\n",
       "        [[5],\n",
       "         [9],\n",
       "         [4],\n",
       "         [2],\n",
       "         [3],\n",
       "         [3],\n",
       "         [6],\n",
       "         [4],\n",
       "         [7],\n",
       "         [9],\n",
       "         [4],\n",
       "         [0],\n",
       "         [5],\n",
       "         [3],\n",
       "         [3],\n",
       "         [1],\n",
       "         [9],\n",
       "         [7],\n",
       "         [6],\n",
       "         [5],\n",
       "         [5],\n",
       "         [0],\n",
       "         [9],\n",
       "         [4],\n",
       "         [4],\n",
       "         [9],\n",
       "         [5],\n",
       "         [2]],\n",
       "\n",
       "        [[6],\n",
       "         [7],\n",
       "         [4],\n",
       "         [4],\n",
       "         [7],\n",
       "         [3],\n",
       "         [3],\n",
       "         [3],\n",
       "         [2],\n",
       "         [7],\n",
       "         [6],\n",
       "         [6],\n",
       "         [0],\n",
       "         [8],\n",
       "         [1],\n",
       "         [7],\n",
       "         [1],\n",
       "         [8],\n",
       "         [0],\n",
       "         [9],\n",
       "         [7],\n",
       "         [3],\n",
       "         [8],\n",
       "         [9],\n",
       "         [3],\n",
       "         [5],\n",
       "         [1],\n",
       "         [4]],\n",
       "\n",
       "        [[5],\n",
       "         [8],\n",
       "         [4],\n",
       "         [6],\n",
       "         [7],\n",
       "         [2],\n",
       "         [8],\n",
       "         [5],\n",
       "         [7],\n",
       "         [2],\n",
       "         [1],\n",
       "         [8],\n",
       "         [2],\n",
       "         [0],\n",
       "         [4],\n",
       "         [4],\n",
       "         [3],\n",
       "         [1],\n",
       "         [8],\n",
       "         [0],\n",
       "         [9],\n",
       "         [5],\n",
       "         [5],\n",
       "         [1],\n",
       "         [0],\n",
       "         [6],\n",
       "         [9],\n",
       "         [7]],\n",
       "\n",
       "        [[0],\n",
       "         [1],\n",
       "         [4],\n",
       "         [9],\n",
       "         [5],\n",
       "         [3],\n",
       "         [6],\n",
       "         [8],\n",
       "         [4],\n",
       "         [7],\n",
       "         [7],\n",
       "         [4],\n",
       "         [4],\n",
       "         [3],\n",
       "         [1],\n",
       "         [8],\n",
       "         [0],\n",
       "         [0],\n",
       "         [1],\n",
       "         [4],\n",
       "         [0],\n",
       "         [8],\n",
       "         [2],\n",
       "         [8],\n",
       "         [3],\n",
       "         [4],\n",
       "         [4],\n",
       "         [9]],\n",
       "\n",
       "        [[0],\n",
       "         [7],\n",
       "         [5],\n",
       "         [9],\n",
       "         [9],\n",
       "         [9],\n",
       "         [3],\n",
       "         [0],\n",
       "         [9],\n",
       "         [3],\n",
       "         [6],\n",
       "         [7],\n",
       "         [4],\n",
       "         [6],\n",
       "         [9],\n",
       "         [3],\n",
       "         [0],\n",
       "         [0],\n",
       "         [6],\n",
       "         [4],\n",
       "         [8],\n",
       "         [7],\n",
       "         [7],\n",
       "         [3],\n",
       "         [4],\n",
       "         [0],\n",
       "         [5],\n",
       "         [7]],\n",
       "\n",
       "        [[1],\n",
       "         [6],\n",
       "         [9],\n",
       "         [1],\n",
       "         [7],\n",
       "         [5],\n",
       "         [6],\n",
       "         [9],\n",
       "         [9],\n",
       "         [2],\n",
       "         [7],\n",
       "         [6],\n",
       "         [2],\n",
       "         [3],\n",
       "         [7],\n",
       "         [9],\n",
       "         [0],\n",
       "         [1],\n",
       "         [7],\n",
       "         [1],\n",
       "         [1],\n",
       "         [0],\n",
       "         [8],\n",
       "         [5],\n",
       "         [1],\n",
       "         [1],\n",
       "         [2],\n",
       "         [7]],\n",
       "\n",
       "        [[0],\n",
       "         [0],\n",
       "         [9],\n",
       "         [7],\n",
       "         [8],\n",
       "         [2],\n",
       "         [6],\n",
       "         [8],\n",
       "         [0],\n",
       "         [6],\n",
       "         [3],\n",
       "         [9],\n",
       "         [5],\n",
       "         [2],\n",
       "         [5],\n",
       "         [4],\n",
       "         [3],\n",
       "         [4],\n",
       "         [8],\n",
       "         [6],\n",
       "         [2],\n",
       "         [6],\n",
       "         [8],\n",
       "         [5],\n",
       "         [1],\n",
       "         [6],\n",
       "         [2],\n",
       "         [2]],\n",
       "\n",
       "        [[9],\n",
       "         [1],\n",
       "         [3],\n",
       "         [5],\n",
       "         [2],\n",
       "         [3],\n",
       "         [9],\n",
       "         [2],\n",
       "         [2],\n",
       "         [9],\n",
       "         [1],\n",
       "         [1],\n",
       "         [3],\n",
       "         [5],\n",
       "         [1],\n",
       "         [3],\n",
       "         [0],\n",
       "         [8],\n",
       "         [0],\n",
       "         [1],\n",
       "         [8],\n",
       "         [2],\n",
       "         [9],\n",
       "         [5],\n",
       "         [6],\n",
       "         [9],\n",
       "         [8],\n",
       "         [3]],\n",
       "\n",
       "        [[7],\n",
       "         [2],\n",
       "         [5],\n",
       "         [8],\n",
       "         [2],\n",
       "         [9],\n",
       "         [4],\n",
       "         [8],\n",
       "         [1],\n",
       "         [5],\n",
       "         [5],\n",
       "         [9],\n",
       "         [4],\n",
       "         [4],\n",
       "         [1],\n",
       "         [1],\n",
       "         [1],\n",
       "         [9],\n",
       "         [0],\n",
       "         [0],\n",
       "         [1],\n",
       "         [8],\n",
       "         [0],\n",
       "         [2],\n",
       "         [7],\n",
       "         [9],\n",
       "         [8],\n",
       "         [6]],\n",
       "\n",
       "        [[7],\n",
       "         [3],\n",
       "         [3],\n",
       "         [8],\n",
       "         [1],\n",
       "         [5],\n",
       "         [9],\n",
       "         [4],\n",
       "         [8],\n",
       "         [0],\n",
       "         [4],\n",
       "         [4],\n",
       "         [5],\n",
       "         [1],\n",
       "         [2],\n",
       "         [2],\n",
       "         [4],\n",
       "         [8],\n",
       "         [5],\n",
       "         [8],\n",
       "         [4],\n",
       "         [6],\n",
       "         [8],\n",
       "         [6],\n",
       "         [0],\n",
       "         [0],\n",
       "         [5],\n",
       "         [2]],\n",
       "\n",
       "        [[2],\n",
       "         [0],\n",
       "         [3],\n",
       "         [7],\n",
       "         [4],\n",
       "         [3],\n",
       "         [2],\n",
       "         [4],\n",
       "         [7],\n",
       "         [5],\n",
       "         [8],\n",
       "         [6],\n",
       "         [6],\n",
       "         [5],\n",
       "         [8],\n",
       "         [2],\n",
       "         [3],\n",
       "         [7],\n",
       "         [9],\n",
       "         [0],\n",
       "         [6],\n",
       "         [4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [8],\n",
       "         [8],\n",
       "         [6],\n",
       "         [0]],\n",
       "\n",
       "        [[4],\n",
       "         [1],\n",
       "         [4],\n",
       "         [1],\n",
       "         [2],\n",
       "         [8],\n",
       "         [0],\n",
       "         [1],\n",
       "         [2],\n",
       "         [7],\n",
       "         [8],\n",
       "         [0],\n",
       "         [5],\n",
       "         [8],\n",
       "         [1],\n",
       "         [0],\n",
       "         [0],\n",
       "         [2],\n",
       "         [7],\n",
       "         [0],\n",
       "         [1],\n",
       "         [3],\n",
       "         [7],\n",
       "         [4],\n",
       "         [6],\n",
       "         [1],\n",
       "         [0],\n",
       "         [7]],\n",
       "\n",
       "        [[6],\n",
       "         [8],\n",
       "         [3],\n",
       "         [2],\n",
       "         [1],\n",
       "         [4],\n",
       "         [9],\n",
       "         [3],\n",
       "         [7],\n",
       "         [8],\n",
       "         [9],\n",
       "         [3],\n",
       "         [5],\n",
       "         [0],\n",
       "         [2],\n",
       "         [3],\n",
       "         [7],\n",
       "         [2],\n",
       "         [0],\n",
       "         [1],\n",
       "         [6],\n",
       "         [5],\n",
       "         [0],\n",
       "         [6],\n",
       "         [7],\n",
       "         [8],\n",
       "         [1],\n",
       "         [4]]]])>"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.expand_dims(x,axis=0) # 高维度之前插入新维度\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=388, shape=(28, 28, 1), dtype=int32, numpy=\n",
       "array([[[9],\n",
       "        [6],\n",
       "        [6],\n",
       "        [9],\n",
       "        [4],\n",
       "        [2],\n",
       "        [1],\n",
       "        [1],\n",
       "        [4],\n",
       "        [1],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [6],\n",
       "        [6],\n",
       "        [7],\n",
       "        [2],\n",
       "        [1],\n",
       "        [9],\n",
       "        [7],\n",
       "        [2],\n",
       "        [8],\n",
       "        [9],\n",
       "        [0],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [9]],\n",
       "\n",
       "       [[9],\n",
       "        [1],\n",
       "        [0],\n",
       "        [8],\n",
       "        [8],\n",
       "        [7],\n",
       "        [3],\n",
       "        [2],\n",
       "        [4],\n",
       "        [2],\n",
       "        [8],\n",
       "        [6],\n",
       "        [4],\n",
       "        [8],\n",
       "        [9],\n",
       "        [6],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [8],\n",
       "        [2],\n",
       "        [3],\n",
       "        [8],\n",
       "        [7],\n",
       "        [6],\n",
       "        [9],\n",
       "        [2],\n",
       "        [6]],\n",
       "\n",
       "       [[0],\n",
       "        [8],\n",
       "        [5],\n",
       "        [7],\n",
       "        [2],\n",
       "        [7],\n",
       "        [5],\n",
       "        [5],\n",
       "        [7],\n",
       "        [1],\n",
       "        [9],\n",
       "        [8],\n",
       "        [3],\n",
       "        [6],\n",
       "        [8],\n",
       "        [8],\n",
       "        [8],\n",
       "        [1],\n",
       "        [1],\n",
       "        [3],\n",
       "        [9],\n",
       "        [3],\n",
       "        [7],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4],\n",
       "        [9],\n",
       "        [3]],\n",
       "\n",
       "       [[3],\n",
       "        [0],\n",
       "        [6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [5],\n",
       "        [0],\n",
       "        [5],\n",
       "        [3],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [7],\n",
       "        [2],\n",
       "        [5],\n",
       "        [6],\n",
       "        [5],\n",
       "        [6],\n",
       "        [4],\n",
       "        [3],\n",
       "        [8],\n",
       "        [1],\n",
       "        [0],\n",
       "        [5],\n",
       "        [1],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5]],\n",
       "\n",
       "       [[6],\n",
       "        [4],\n",
       "        [4],\n",
       "        [6],\n",
       "        [5],\n",
       "        [1],\n",
       "        [7],\n",
       "        [9],\n",
       "        [7],\n",
       "        [8],\n",
       "        [3],\n",
       "        [4],\n",
       "        [7],\n",
       "        [9],\n",
       "        [1],\n",
       "        [3],\n",
       "        [4],\n",
       "        [7],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5],\n",
       "        [4],\n",
       "        [0],\n",
       "        [8],\n",
       "        [4],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4]],\n",
       "\n",
       "       [[9],\n",
       "        [9],\n",
       "        [0],\n",
       "        [4],\n",
       "        [7],\n",
       "        [9],\n",
       "        [9],\n",
       "        [1],\n",
       "        [9],\n",
       "        [6],\n",
       "        [3],\n",
       "        [2],\n",
       "        [0],\n",
       "        [0],\n",
       "        [6],\n",
       "        [9],\n",
       "        [2],\n",
       "        [6],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [2],\n",
       "        [0],\n",
       "        [0],\n",
       "        [8],\n",
       "        [2],\n",
       "        [8]],\n",
       "\n",
       "       [[7],\n",
       "        [0],\n",
       "        [0],\n",
       "        [7],\n",
       "        [6],\n",
       "        [7],\n",
       "        [3],\n",
       "        [6],\n",
       "        [9],\n",
       "        [8],\n",
       "        [9],\n",
       "        [0],\n",
       "        [3],\n",
       "        [1],\n",
       "        [5],\n",
       "        [4],\n",
       "        [9],\n",
       "        [2],\n",
       "        [9],\n",
       "        [2],\n",
       "        [7],\n",
       "        [8],\n",
       "        [0],\n",
       "        [2],\n",
       "        [7],\n",
       "        [4],\n",
       "        [3],\n",
       "        [9]],\n",
       "\n",
       "       [[3],\n",
       "        [5],\n",
       "        [6],\n",
       "        [4],\n",
       "        [9],\n",
       "        [7],\n",
       "        [3],\n",
       "        [8],\n",
       "        [3],\n",
       "        [9],\n",
       "        [1],\n",
       "        [6],\n",
       "        [2],\n",
       "        [6],\n",
       "        [3],\n",
       "        [7],\n",
       "        [2],\n",
       "        [7],\n",
       "        [0],\n",
       "        [8],\n",
       "        [2],\n",
       "        [2],\n",
       "        [4],\n",
       "        [5],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [0]],\n",
       "\n",
       "       [[6],\n",
       "        [3],\n",
       "        [0],\n",
       "        [2],\n",
       "        [3],\n",
       "        [2],\n",
       "        [5],\n",
       "        [8],\n",
       "        [7],\n",
       "        [2],\n",
       "        [2],\n",
       "        [6],\n",
       "        [8],\n",
       "        [6],\n",
       "        [2],\n",
       "        [0],\n",
       "        [3],\n",
       "        [6],\n",
       "        [3],\n",
       "        [6],\n",
       "        [5],\n",
       "        [6],\n",
       "        [7],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5],\n",
       "        [5],\n",
       "        [1]],\n",
       "\n",
       "       [[5],\n",
       "        [5],\n",
       "        [1],\n",
       "        [6],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [5],\n",
       "        [6],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [2],\n",
       "        [9],\n",
       "        [3],\n",
       "        [2],\n",
       "        [1],\n",
       "        [2],\n",
       "        [8],\n",
       "        [5],\n",
       "        [6],\n",
       "        [2],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [5],\n",
       "        [4],\n",
       "        [6]],\n",
       "\n",
       "       [[9],\n",
       "        [2],\n",
       "        [6],\n",
       "        [3],\n",
       "        [4],\n",
       "        [3],\n",
       "        [6],\n",
       "        [9],\n",
       "        [6],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [7],\n",
       "        [9],\n",
       "        [2],\n",
       "        [3],\n",
       "        [1],\n",
       "        [2],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [3],\n",
       "        [9],\n",
       "        [2],\n",
       "        [5],\n",
       "        [4]],\n",
       "\n",
       "       [[2],\n",
       "        [1],\n",
       "        [3],\n",
       "        [7],\n",
       "        [7],\n",
       "        [6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [8],\n",
       "        [3],\n",
       "        [7],\n",
       "        [9],\n",
       "        [6],\n",
       "        [9],\n",
       "        [3],\n",
       "        [2],\n",
       "        [6],\n",
       "        [9],\n",
       "        [1],\n",
       "        [1],\n",
       "        [0],\n",
       "        [6],\n",
       "        [4],\n",
       "        [5],\n",
       "        [8],\n",
       "        [4],\n",
       "        [6]],\n",
       "\n",
       "       [[0],\n",
       "        [7],\n",
       "        [7],\n",
       "        [2],\n",
       "        [4],\n",
       "        [7],\n",
       "        [2],\n",
       "        [6],\n",
       "        [4],\n",
       "        [4],\n",
       "        [1],\n",
       "        [1],\n",
       "        [7],\n",
       "        [0],\n",
       "        [2],\n",
       "        [6],\n",
       "        [6],\n",
       "        [0],\n",
       "        [1],\n",
       "        [3],\n",
       "        [0],\n",
       "        [2],\n",
       "        [2],\n",
       "        [6],\n",
       "        [4],\n",
       "        [9],\n",
       "        [3],\n",
       "        [1]],\n",
       "\n",
       "       [[3],\n",
       "        [5],\n",
       "        [0],\n",
       "        [4],\n",
       "        [0],\n",
       "        [3],\n",
       "        [5],\n",
       "        [7],\n",
       "        [3],\n",
       "        [4],\n",
       "        [3],\n",
       "        [2],\n",
       "        [3],\n",
       "        [2],\n",
       "        [4],\n",
       "        [0],\n",
       "        [7],\n",
       "        [7],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [1],\n",
       "        [2],\n",
       "        [2],\n",
       "        [4],\n",
       "        [4],\n",
       "        [2],\n",
       "        [6]],\n",
       "\n",
       "       [[8],\n",
       "        [0],\n",
       "        [4],\n",
       "        [1],\n",
       "        [6],\n",
       "        [7],\n",
       "        [2],\n",
       "        [0],\n",
       "        [2],\n",
       "        [8],\n",
       "        [1],\n",
       "        [5],\n",
       "        [5],\n",
       "        [1],\n",
       "        [7],\n",
       "        [0],\n",
       "        [2],\n",
       "        [6],\n",
       "        [9],\n",
       "        [3],\n",
       "        [6],\n",
       "        [4],\n",
       "        [0],\n",
       "        [9],\n",
       "        [0],\n",
       "        [5],\n",
       "        [5],\n",
       "        [7]],\n",
       "\n",
       "       [[5],\n",
       "        [9],\n",
       "        [4],\n",
       "        [2],\n",
       "        [3],\n",
       "        [3],\n",
       "        [6],\n",
       "        [4],\n",
       "        [7],\n",
       "        [9],\n",
       "        [4],\n",
       "        [0],\n",
       "        [5],\n",
       "        [3],\n",
       "        [3],\n",
       "        [1],\n",
       "        [9],\n",
       "        [7],\n",
       "        [6],\n",
       "        [5],\n",
       "        [5],\n",
       "        [0],\n",
       "        [9],\n",
       "        [4],\n",
       "        [4],\n",
       "        [9],\n",
       "        [5],\n",
       "        [2]],\n",
       "\n",
       "       [[6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [7],\n",
       "        [3],\n",
       "        [3],\n",
       "        [3],\n",
       "        [2],\n",
       "        [7],\n",
       "        [6],\n",
       "        [6],\n",
       "        [0],\n",
       "        [8],\n",
       "        [1],\n",
       "        [7],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [9],\n",
       "        [7],\n",
       "        [3],\n",
       "        [8],\n",
       "        [9],\n",
       "        [3],\n",
       "        [5],\n",
       "        [1],\n",
       "        [4]],\n",
       "\n",
       "       [[5],\n",
       "        [8],\n",
       "        [4],\n",
       "        [6],\n",
       "        [7],\n",
       "        [2],\n",
       "        [8],\n",
       "        [5],\n",
       "        [7],\n",
       "        [2],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [0],\n",
       "        [4],\n",
       "        [4],\n",
       "        [3],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [9],\n",
       "        [5],\n",
       "        [5],\n",
       "        [1],\n",
       "        [0],\n",
       "        [6],\n",
       "        [9],\n",
       "        [7]],\n",
       "\n",
       "       [[0],\n",
       "        [1],\n",
       "        [4],\n",
       "        [9],\n",
       "        [5],\n",
       "        [3],\n",
       "        [6],\n",
       "        [8],\n",
       "        [4],\n",
       "        [7],\n",
       "        [7],\n",
       "        [4],\n",
       "        [4],\n",
       "        [3],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [0],\n",
       "        [1],\n",
       "        [4],\n",
       "        [0],\n",
       "        [8],\n",
       "        [2],\n",
       "        [8],\n",
       "        [3],\n",
       "        [4],\n",
       "        [4],\n",
       "        [9]],\n",
       "\n",
       "       [[0],\n",
       "        [7],\n",
       "        [5],\n",
       "        [9],\n",
       "        [9],\n",
       "        [9],\n",
       "        [3],\n",
       "        [0],\n",
       "        [9],\n",
       "        [3],\n",
       "        [6],\n",
       "        [7],\n",
       "        [4],\n",
       "        [6],\n",
       "        [9],\n",
       "        [3],\n",
       "        [0],\n",
       "        [0],\n",
       "        [6],\n",
       "        [4],\n",
       "        [8],\n",
       "        [7],\n",
       "        [7],\n",
       "        [3],\n",
       "        [4],\n",
       "        [0],\n",
       "        [5],\n",
       "        [7]],\n",
       "\n",
       "       [[1],\n",
       "        [6],\n",
       "        [9],\n",
       "        [1],\n",
       "        [7],\n",
       "        [5],\n",
       "        [6],\n",
       "        [9],\n",
       "        [9],\n",
       "        [2],\n",
       "        [7],\n",
       "        [6],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [9],\n",
       "        [0],\n",
       "        [1],\n",
       "        [7],\n",
       "        [1],\n",
       "        [1],\n",
       "        [0],\n",
       "        [8],\n",
       "        [5],\n",
       "        [1],\n",
       "        [1],\n",
       "        [2],\n",
       "        [7]],\n",
       "\n",
       "       [[0],\n",
       "        [0],\n",
       "        [9],\n",
       "        [7],\n",
       "        [8],\n",
       "        [2],\n",
       "        [6],\n",
       "        [8],\n",
       "        [0],\n",
       "        [6],\n",
       "        [3],\n",
       "        [9],\n",
       "        [5],\n",
       "        [2],\n",
       "        [5],\n",
       "        [4],\n",
       "        [3],\n",
       "        [4],\n",
       "        [8],\n",
       "        [6],\n",
       "        [2],\n",
       "        [6],\n",
       "        [8],\n",
       "        [5],\n",
       "        [1],\n",
       "        [6],\n",
       "        [2],\n",
       "        [2]],\n",
       "\n",
       "       [[9],\n",
       "        [1],\n",
       "        [3],\n",
       "        [5],\n",
       "        [2],\n",
       "        [3],\n",
       "        [9],\n",
       "        [2],\n",
       "        [2],\n",
       "        [9],\n",
       "        [1],\n",
       "        [1],\n",
       "        [3],\n",
       "        [5],\n",
       "        [1],\n",
       "        [3],\n",
       "        [0],\n",
       "        [8],\n",
       "        [0],\n",
       "        [1],\n",
       "        [8],\n",
       "        [2],\n",
       "        [9],\n",
       "        [5],\n",
       "        [6],\n",
       "        [9],\n",
       "        [8],\n",
       "        [3]],\n",
       "\n",
       "       [[7],\n",
       "        [2],\n",
       "        [5],\n",
       "        [8],\n",
       "        [2],\n",
       "        [9],\n",
       "        [4],\n",
       "        [8],\n",
       "        [1],\n",
       "        [5],\n",
       "        [5],\n",
       "        [9],\n",
       "        [4],\n",
       "        [4],\n",
       "        [1],\n",
       "        [1],\n",
       "        [1],\n",
       "        [9],\n",
       "        [0],\n",
       "        [0],\n",
       "        [1],\n",
       "        [8],\n",
       "        [0],\n",
       "        [2],\n",
       "        [7],\n",
       "        [9],\n",
       "        [8],\n",
       "        [6]],\n",
       "\n",
       "       [[7],\n",
       "        [3],\n",
       "        [3],\n",
       "        [8],\n",
       "        [1],\n",
       "        [5],\n",
       "        [9],\n",
       "        [4],\n",
       "        [8],\n",
       "        [0],\n",
       "        [4],\n",
       "        [4],\n",
       "        [5],\n",
       "        [1],\n",
       "        [2],\n",
       "        [2],\n",
       "        [4],\n",
       "        [8],\n",
       "        [5],\n",
       "        [8],\n",
       "        [4],\n",
       "        [6],\n",
       "        [8],\n",
       "        [6],\n",
       "        [0],\n",
       "        [0],\n",
       "        [5],\n",
       "        [2]],\n",
       "\n",
       "       [[2],\n",
       "        [0],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [3],\n",
       "        [2],\n",
       "        [4],\n",
       "        [7],\n",
       "        [5],\n",
       "        [8],\n",
       "        [6],\n",
       "        [6],\n",
       "        [5],\n",
       "        [8],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [9],\n",
       "        [0],\n",
       "        [6],\n",
       "        [4],\n",
       "        [6],\n",
       "        [5],\n",
       "        [8],\n",
       "        [8],\n",
       "        [6],\n",
       "        [0]],\n",
       "\n",
       "       [[4],\n",
       "        [1],\n",
       "        [4],\n",
       "        [1],\n",
       "        [2],\n",
       "        [8],\n",
       "        [0],\n",
       "        [1],\n",
       "        [2],\n",
       "        [7],\n",
       "        [8],\n",
       "        [0],\n",
       "        [5],\n",
       "        [8],\n",
       "        [1],\n",
       "        [0],\n",
       "        [0],\n",
       "        [2],\n",
       "        [7],\n",
       "        [0],\n",
       "        [1],\n",
       "        [3],\n",
       "        [7],\n",
       "        [4],\n",
       "        [6],\n",
       "        [1],\n",
       "        [0],\n",
       "        [7]],\n",
       "\n",
       "       [[6],\n",
       "        [8],\n",
       "        [3],\n",
       "        [2],\n",
       "        [1],\n",
       "        [4],\n",
       "        [9],\n",
       "        [3],\n",
       "        [7],\n",
       "        [8],\n",
       "        [9],\n",
       "        [3],\n",
       "        [5],\n",
       "        [0],\n",
       "        [2],\n",
       "        [3],\n",
       "        [7],\n",
       "        [2],\n",
       "        [0],\n",
       "        [1],\n",
       "        [6],\n",
       "        [5],\n",
       "        [0],\n",
       "        [6],\n",
       "        [7],\n",
       "        [8],\n",
       "        [1],\n",
       "        [4]]])>"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.squeeze(x, axis=0) # 删除图片数量维度\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=393, shape=(28, 28), dtype=int32, numpy=\n",
       "array([[0, 0, 4, 5, 6, 5, 3, 9, 4, 0, 8, 2, 4, 4, 7, 1, 4, 0, 9, 7, 1, 7,\n",
       "        4, 0, 6, 8, 7, 4],\n",
       "       [4, 6, 8, 2, 8, 7, 3, 1, 0, 9, 4, 6, 6, 9, 3, 4, 5, 2, 9, 2, 9, 7,\n",
       "        6, 9, 4, 5, 8, 3],\n",
       "       [7, 1, 1, 2, 7, 2, 3, 6, 9, 1, 5, 6, 4, 5, 9, 2, 8, 7, 6, 5, 7, 1,\n",
       "        8, 2, 5, 3, 3, 9],\n",
       "       [4, 7, 1, 2, 7, 2, 4, 1, 7, 4, 2, 4, 6, 5, 7, 7, 3, 6, 8, 9, 8, 6,\n",
       "        1, 7, 4, 0, 2, 0],\n",
       "       [2, 7, 4, 4, 4, 9, 2, 7, 9, 6, 5, 0, 2, 2, 5, 5, 6, 0, 0, 0, 7, 7,\n",
       "        0, 0, 2, 6, 2, 1],\n",
       "       [2, 0, 8, 6, 6, 8, 3, 9, 1, 6, 2, 3, 1, 8, 9, 0, 0, 5, 4, 9, 8, 3,\n",
       "        5, 3, 5, 4, 8, 7],\n",
       "       [5, 9, 3, 3, 7, 9, 5, 4, 6, 8, 6, 2, 4, 9, 3, 7, 8, 0, 9, 1, 7, 0,\n",
       "        5, 1, 7, 7, 3, 9],\n",
       "       [6, 7, 8, 3, 1, 2, 2, 4, 4, 5, 3, 0, 8, 1, 8, 6, 6, 5, 1, 5, 5, 8,\n",
       "        6, 8, 7, 8, 0, 7],\n",
       "       [8, 3, 0, 8, 8, 4, 3, 4, 6, 2, 6, 3, 2, 5, 6, 1, 3, 0, 6, 5, 9, 3,\n",
       "        2, 5, 5, 8, 6, 9],\n",
       "       [5, 2, 8, 0, 0, 9, 0, 2, 1, 7, 7, 4, 4, 9, 5, 1, 3, 7, 5, 2, 4, 0,\n",
       "        1, 9, 3, 7, 1, 4],\n",
       "       [9, 6, 2, 8, 5, 0, 8, 1, 2, 4, 0, 2, 8, 2, 9, 1, 0, 8, 1, 5, 2, 5,\n",
       "        8, 2, 6, 0, 9, 0],\n",
       "       [5, 4, 9, 8, 4, 5, 1, 8, 4, 6, 0, 0, 3, 8, 9, 5, 9, 8, 6, 3, 0, 8,\n",
       "        3, 1, 2, 2, 4, 4],\n",
       "       [6, 1, 1, 1, 8, 7, 3, 6, 0, 4, 5, 0, 7, 0, 0, 5, 1, 4, 4, 8, 4, 8,\n",
       "        3, 6, 7, 7, 4, 7],\n",
       "       [8, 2, 4, 8, 7, 1, 4, 8, 0, 4, 2, 8, 2, 6, 5, 4, 8, 9, 7, 7, 3, 3,\n",
       "        1, 6, 7, 6, 2, 7],\n",
       "       [5, 6, 4, 2, 4, 5, 3, 8, 0, 8, 3, 0, 2, 5, 6, 7, 4, 2, 4, 9, 8, 4,\n",
       "        3, 4, 8, 9, 9, 9],\n",
       "       [0, 5, 5, 7, 4, 9, 8, 7, 7, 9, 5, 4, 1, 2, 1, 2, 8, 0, 7, 5, 5, 1,\n",
       "        0, 0, 2, 0, 3, 8],\n",
       "       [0, 2, 8, 0, 5, 9, 3, 8, 0, 2, 4, 3, 4, 5, 3, 6, 6, 3, 2, 9, 6, 0,\n",
       "        1, 7, 6, 2, 4, 4],\n",
       "       [6, 3, 6, 0, 8, 2, 9, 2, 0, 5, 8, 2, 1, 7, 4, 7, 2, 3, 8, 3, 0, 2,\n",
       "        2, 6, 0, 6, 1, 7],\n",
       "       [9, 7, 5, 2, 3, 8, 6, 0, 3, 5, 5, 1, 1, 0, 5, 2, 8, 1, 9, 6, 7, 5,\n",
       "        0, 1, 1, 1, 4, 2],\n",
       "       [5, 2, 2, 7, 5, 2, 3, 4, 4, 2, 6, 3, 9, 1, 3, 4, 7, 7, 0, 4, 1, 1,\n",
       "        3, 4, 9, 5, 8, 7],\n",
       "       [7, 0, 2, 1, 4, 7, 8, 0, 4, 1, 1, 2, 8, 0, 9, 7, 9, 5, 8, 5, 4, 5,\n",
       "        1, 9, 8, 9, 5, 3],\n",
       "       [6, 4, 1, 3, 2, 5, 4, 5, 4, 7, 6, 2, 1, 3, 9, 3, 1, 8, 2, 2, 9, 6,\n",
       "        9, 0, 5, 1, 0, 0],\n",
       "       [0, 1, 9, 6, 0, 1, 4, 5, 8, 8, 7, 3, 8, 3, 5, 9, 8, 2, 3, 5, 3, 9,\n",
       "        7, 2, 7, 1, 4, 6],\n",
       "       [4, 5, 7, 7, 3, 2, 1, 4, 2, 5, 2, 8, 9, 1, 5, 7, 8, 0, 4, 9, 5, 9,\n",
       "        0, 3, 8, 8, 9, 7],\n",
       "       [5, 3, 9, 0, 3, 3, 8, 2, 7, 1, 0, 4, 3, 8, 9, 0, 7, 1, 4, 5, 8, 4,\n",
       "        4, 5, 5, 8, 5, 5],\n",
       "       [8, 1, 9, 4, 0, 7, 9, 5, 7, 9, 7, 2, 8, 6, 6, 6, 2, 4, 4, 1, 9, 0,\n",
       "        5, 7, 5, 7, 5, 6],\n",
       "       [0, 7, 0, 5, 0, 2, 9, 9, 5, 3, 8, 8, 2, 0, 3, 3, 7, 7, 6, 6, 0, 9,\n",
       "        4, 0, 3, 8, 6, 3],\n",
       "       [6, 1, 5, 6, 7, 9, 2, 3, 4, 5, 3, 9, 5, 0, 5, 3, 5, 4, 9, 8, 0, 7,\n",
       "        2, 0, 1, 7, 8, 8]])>"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.random.uniform([1,28,28,1],maxval=10,dtype=tf.int32)\n",
    "tf.squeeze(x) # 删除所有长度为 1 的维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 交换维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=401, shape=(2, 3, 32, 32), dtype=float32, numpy=\n",
       "array([[[[-4.6426612e-01,  6.6180402e-01,  4.6666557e-01, ...,\n",
       "           2.9917407e-01,  6.1010522e-01, -2.7273178e-01],\n",
       "         [-1.0964775e+00,  7.0370480e-02, -1.4398359e+00, ...,\n",
       "          -1.3319954e-01,  5.9592605e-01, -5.3394008e-01],\n",
       "         [-1.8518368e+00,  1.9056295e+00,  8.8404858e-01, ...,\n",
       "           3.3489071e-02,  2.7276034e+00,  6.2328357e-01],\n",
       "         ...,\n",
       "         [ 5.2914214e-01, -1.8093091e+00, -3.7111488e-01, ...,\n",
       "           9.5485711e-01, -2.0736468e+00, -5.8235383e-01],\n",
       "         [-9.1656226e-01, -7.2701806e-01,  1.1805288e-01, ...,\n",
       "           6.2868679e-01, -9.1965991e-01, -1.4314998e+00],\n",
       "         [ 1.4785954e+00,  1.0433403e+00,  1.4817585e+00, ...,\n",
       "           1.4287323e+00, -1.4297609e+00, -1.0148525e-01]],\n",
       "\n",
       "        [[ 8.5222179e-01, -8.9675528e-01,  5.0976098e-01, ...,\n",
       "          -9.5692623e-01, -7.9007179e-01, -5.0860822e-01],\n",
       "         [ 1.7652961e+00, -1.0947875e+00, -9.0407258e-01, ...,\n",
       "          -1.1809442e+00,  2.6283944e-01,  1.5434856e+00],\n",
       "         [ 7.9349443e-02,  9.7528082e-01, -7.8024137e-01, ...,\n",
       "          -7.6358777e-01,  2.0054133e+00, -4.6544132e-01],\n",
       "         ...,\n",
       "         [-1.2935880e+00,  1.6400665e+00,  9.2608613e-01, ...,\n",
       "           8.2730734e-01, -3.9230233e-01,  1.9720900e-01],\n",
       "         [-1.0606921e+00, -1.8570166e-02,  6.2905580e-01, ...,\n",
       "          -4.8078519e-01,  4.2413775e-02, -3.8581219e-01],\n",
       "         [ 6.3269907e-01, -7.3165691e-01, -5.5905408e-01, ...,\n",
       "          -1.9426510e-01,  1.3828083e+00, -1.2955737e+00]],\n",
       "\n",
       "        [[-3.6340144e-02, -5.7036912e-01, -2.3264660e-01, ...,\n",
       "           2.3870924e+00,  4.5839351e-01, -2.7810646e-02],\n",
       "         [ 1.2090608e+00,  1.7820239e-01, -1.6029425e+00, ...,\n",
       "          -1.3560791e-01, -9.2261828e-02,  1.3753730e+00],\n",
       "         [-2.2692544e+00, -5.8561254e-02, -7.5177747e-01, ...,\n",
       "          -4.0710047e-01,  1.5057471e-01, -7.4934942e-01],\n",
       "         ...,\n",
       "         [-1.3274254e+00, -7.8089839e-01,  1.4004223e-01, ...,\n",
       "          -2.6660532e-01, -6.2221289e-01,  1.3341151e+00],\n",
       "         [ 5.3816432e-01,  1.1841062e+00,  1.5229483e+00, ...,\n",
       "          -1.0829147e+00,  6.0922092e-01, -9.4298124e-01],\n",
       "         [ 7.5882441e-01, -8.4186956e-02, -1.5552664e+00, ...,\n",
       "          -2.9759398e-01,  7.1727276e-01, -4.1592693e-01]]],\n",
       "\n",
       "\n",
       "       [[[ 2.3423624e-03, -1.3762254e+00, -5.8343571e-01, ...,\n",
       "           3.2136872e-01, -6.4766157e-01, -6.1397547e-01],\n",
       "         [-9.2026639e-01,  1.0188217e+00,  1.6549262e-01, ...,\n",
       "           1.9655170e+00, -8.1465387e-01, -4.1974481e-02],\n",
       "         [-3.2081196e+00,  2.0641302e-01, -1.1942104e+00, ...,\n",
       "          -5.2823025e-01,  4.2250064e-01, -1.7318517e+00],\n",
       "         ...,\n",
       "         [-5.7422912e-01, -1.2860565e+00, -4.8207164e-01, ...,\n",
       "           7.0420420e-01, -4.8974380e-01, -1.1782004e-01],\n",
       "         [-3.4544575e-01, -5.2156734e-01,  1.0303683e+00, ...,\n",
       "           1.6032342e+00, -8.8012552e-01,  6.9457179e-01],\n",
       "         [-1.7362752e+00, -1.4003431e+00,  1.0904669e+00, ...,\n",
       "          -6.9761282e-01, -4.1872451e-01, -1.4075574e+00]],\n",
       "\n",
       "        [[ 3.4800956e-01,  6.8777114e-01, -7.8013980e-01, ...,\n",
       "          -9.8967545e-02,  4.6222955e-01, -4.3665543e-01],\n",
       "         [ 7.8116812e-02,  7.9885596e-01, -1.5046658e-01, ...,\n",
       "           5.9969872e-01, -2.7182418e-01,  6.0119981e-01],\n",
       "         [-3.4708211e-01,  1.4841856e+00,  9.9988461e-01, ...,\n",
       "          -1.0023893e+00,  2.0601603e-01, -1.8716424e+00],\n",
       "         ...,\n",
       "         [-8.8126367e-01,  4.1712517e-01,  1.1101336e+00, ...,\n",
       "           1.0007132e-01,  1.9968381e+00, -1.6255676e+00],\n",
       "         [ 2.2943366e+00, -5.9404498e-01,  8.3433759e-01, ...,\n",
       "           8.1292218e-01,  2.5192448e-01, -4.9088755e-01],\n",
       "         [ 9.4806338e-03,  2.1625957e+00,  5.9272236e-01, ...,\n",
       "          -1.8650712e+00,  6.1104679e-01,  1.8304964e+00]],\n",
       "\n",
       "        [[-9.1230589e-01,  1.0160280e+00, -1.1168532e+00, ...,\n",
       "           1.4157888e+00,  6.5606695e-01, -9.1677308e-01],\n",
       "         [-1.3329383e+00, -5.7347447e-01,  9.2291957e-01, ...,\n",
       "          -7.7254945e-01,  6.3247508e-01, -1.0011988e+00],\n",
       "         [-3.4182516e-01,  3.4510401e-01, -9.4687492e-01, ...,\n",
       "           1.1428174e+00, -6.9130319e-01,  5.5755210e-01],\n",
       "         ...,\n",
       "         [-1.2695917e-01,  2.3106803e-01, -1.2998368e+00, ...,\n",
       "          -7.1483359e-02,  1.7072452e+00, -1.4966367e-01],\n",
       "         [-4.9037117e-01,  1.8909870e-01, -2.2295935e-01, ...,\n",
       "           8.5783911e-01, -1.4391050e+00, -5.4294819e-01],\n",
       "         [ 9.3245429e-01, -1.9139901e-01,  4.1055971e-01, ...,\n",
       "           6.1818069e-01, -4.3548179e-01,  1.3763354e+00]]]],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.random.normal([2,32,32,3])\n",
    "# 交换维度\n",
    "tf.transpose(x,perm=[0,3,1,2]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=409, shape=(2, 32, 32, 3), dtype=float32, numpy=\n",
       "array([[[[-1.8452803 ,  2.0333989 , -0.0535989 ],\n",
       "         [-0.25328714,  1.5190583 ,  0.48563102],\n",
       "         [ 0.59447205,  0.19083412, -0.95437056],\n",
       "         ...,\n",
       "         [ 1.5498217 , -0.9326852 ,  1.3211845 ],\n",
       "         [ 0.8097577 ,  2.6648786 , -1.4913405 ],\n",
       "         [ 1.2091789 , -2.6485445 , -0.32557693]],\n",
       "\n",
       "        [[-0.45189056,  0.54656315, -1.3605549 ],\n",
       "         [ 0.09748429,  1.534225  , -0.7361736 ],\n",
       "         [-0.582725  , -0.6886578 ,  0.36123005],\n",
       "         ...,\n",
       "         [ 0.32123768, -0.444655  ,  1.4588033 ],\n",
       "         [ 1.7372004 , -0.34687608,  0.03764085],\n",
       "         [ 0.3742446 ,  0.17379837, -0.5747571 ]],\n",
       "\n",
       "        [[-0.8977981 ,  0.65956086,  1.6590528 ],\n",
       "         [-0.70590645, -0.6269749 ,  0.11985448],\n",
       "         [ 1.5120564 ,  0.3294992 , -0.5916248 ],\n",
       "         ...,\n",
       "         [-0.25741085,  0.00957887,  0.06577269],\n",
       "         [ 1.3285046 , -0.22772112, -1.312285  ],\n",
       "         [ 2.1890569 ,  0.44651207,  0.7327839 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-0.14295377,  1.7048007 , -0.0429301 ],\n",
       "         [-0.8619474 ,  1.5029602 ,  1.7299842 ],\n",
       "         [-1.1418179 , -1.3208891 , -0.11002917],\n",
       "         ...,\n",
       "         [-1.3145828 ,  0.09465664, -0.7233533 ],\n",
       "         [ 0.18692811,  1.1603885 ,  2.712416  ],\n",
       "         [ 0.35319018, -0.73869324,  1.4951551 ]],\n",
       "\n",
       "        [[-0.9175976 , -0.37555432, -2.905311  ],\n",
       "         [-1.2621356 , -0.32713035,  0.6552673 ],\n",
       "         [-1.3816463 ,  1.8345401 , -0.51941437],\n",
       "         ...,\n",
       "         [ 1.4524697 , -0.15577967, -1.8955196 ],\n",
       "         [-1.3418065 ,  1.0843124 ,  0.29393134],\n",
       "         [ 0.20196787, -0.26264763, -0.32500327]],\n",
       "\n",
       "        [[-1.1818362 , -0.33836573, -0.871514  ],\n",
       "         [-1.176203  ,  0.7162369 , -0.11985523],\n",
       "         [-1.3277929 , -0.6467968 ,  0.4808952 ],\n",
       "         ...,\n",
       "         [ 0.1504531 ,  0.202779  , -0.73306954],\n",
       "         [-0.15733747,  1.0198284 ,  1.2955364 ],\n",
       "         [ 0.04783008,  0.389798  ,  0.22250667]]],\n",
       "\n",
       "\n",
       "       [[[ 1.5470157 ,  0.35414594,  1.2828164 ],\n",
       "         [-0.64362633, -0.4236902 , -0.7602445 ],\n",
       "         [ 1.1662749 , -0.9286629 , -0.22158153],\n",
       "         ...,\n",
       "         [ 0.90983886, -1.6837937 ,  0.05412139],\n",
       "         [ 0.13126732,  0.12183326, -0.64553076],\n",
       "         [ 1.1887455 ,  0.41225022, -0.01291056]],\n",
       "\n",
       "        [[-0.74410325,  0.15881743,  0.25956872],\n",
       "         [ 0.4288989 , -1.3120923 , -0.48465595],\n",
       "         [-0.9207494 ,  0.6639962 ,  0.53212106],\n",
       "         ...,\n",
       "         [-0.9711581 ,  0.02938701,  1.04667   ],\n",
       "         [-1.9822342 , -2.0204124 , -0.4328782 ],\n",
       "         [-1.0276635 ,  0.95280063,  1.6458342 ]],\n",
       "\n",
       "        [[ 0.5589305 ,  0.79166394,  0.2643017 ],\n",
       "         [ 0.40286106, -1.1994376 , -0.70340186],\n",
       "         [ 0.3887804 , -0.44508982,  1.6922891 ],\n",
       "         ...,\n",
       "         [-2.4328902 , -0.9893422 , -1.6799333 ],\n",
       "         [-0.7544474 ,  0.7419811 ,  1.352145  ],\n",
       "         [ 0.7132659 , -0.13223821, -0.6860259 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 1.0637616 , -0.37059924, -0.68205446],\n",
       "         [-1.8524883 ,  0.12070003, -0.11133374],\n",
       "         [ 0.35854694, -0.1048164 ,  0.82541245],\n",
       "         ...,\n",
       "         [ 1.7367653 , -0.36312464, -0.66890967],\n",
       "         [ 1.3027514 ,  1.704878  , -0.8292307 ],\n",
       "         [-0.8331758 ,  2.3091748 ,  1.2745844 ]],\n",
       "\n",
       "        [[-0.7425056 ,  0.14594132, -0.08328532],\n",
       "         [-0.72126305,  0.652093  , -0.2809146 ],\n",
       "         [-1.2918764 ,  1.4513303 , -0.09210815],\n",
       "         ...,\n",
       "         [-1.3232245 , -0.24373382,  0.66719306],\n",
       "         [ 0.9036088 ,  1.3696408 ,  0.5449671 ],\n",
       "         [-1.3852375 , -0.22977807,  0.3171226 ]],\n",
       "\n",
       "        [[-0.21107052, -0.8547733 ,  0.8437949 ],\n",
       "         [-0.19685602,  0.13772078, -0.18744624],\n",
       "         [-0.23252137,  1.1381994 , -0.43618867],\n",
       "         ...,\n",
       "         [ 0.39206254,  0.46994674,  0.76433045],\n",
       "         [ 1.3153771 , -0.47446647,  0.187829  ],\n",
       "         [ 0.05177504, -1.6043636 ,  0.46883655]]]], dtype=float32)>"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.random.normal([2,32,32,3])\n",
    "# 交换维度\n",
    "tf.transpose(x,perm=[0,2,1,3]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 复制数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=412, shape=(1, 2), dtype=int32, numpy=array([[1, 2]])>"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建向量 b\n",
    "b = tf.constant([1,2]) \n",
    "# 插入新维度，变成矩阵\n",
    "b = tf.expand_dims(b, axis=0) \n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=414, shape=(2, 2), dtype=int32, numpy=\n",
       "array([[1, 2],\n",
       "       [1, 2]])>"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本维度上复制一份\n",
    "b = tf.tile(b, multiples=[2,1]) \n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=420, shape=(2, 2), dtype=int32, numpy=\n",
       "array([[0, 1],\n",
       "       [2, 3]])>"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.range(4)\n",
    "# 创建 2 行 2 列矩阵\n",
    "x=tf.reshape(x,[2,2]) \n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=422, shape=(2, 4), dtype=int32, numpy=\n",
       "array([[0, 1, 0, 1],\n",
       "       [2, 3, 2, 3]])>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列维度复制一份\n",
    "x = tf.tile(x,multiples=[1,2]) \n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=424, shape=(4, 4), dtype=int32, numpy=\n",
       "array([[0, 1, 0, 1],\n",
       "       [2, 3, 2, 3],\n",
       "       [0, 1, 0, 1],\n",
       "       [2, 3, 2, 3]])>"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行维度复制一份\n",
    "x = tf.tile(x,multiples=[2,1]) \n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Broadcasting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=432, shape=(2, 32, 32, 3), dtype=float32, numpy=\n",
       "array([[[[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]]],\n",
       "\n",
       "\n",
       "       [[[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]],\n",
       "\n",
       "        [[ 1.477565  ,  1.477565  ,  1.477565  ],\n",
       "         [-0.24481297, -0.24481297, -0.24481297],\n",
       "         [ 0.221323  ,  0.221323  ,  0.221323  ],\n",
       "         ...,\n",
       "         [-0.4889467 , -0.4889467 , -0.4889467 ],\n",
       "         [ 0.39240143,  0.39240143,  0.39240143],\n",
       "         [-1.0636833 , -1.0636833 , -1.0636833 ]]]], dtype=float32)>"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建矩阵\n",
    "A = tf.random.normal([32,1]) \n",
    "# 扩展为 4D 张量\n",
    "tf.broadcast_to(A, [2,32,32,3]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Incompatible shapes: [32,2] vs. [2,32,32,4] [Op:BroadcastTo]\n"
     ]
    }
   ],
   "source": [
    "A = tf.random.normal([32,2])\n",
    "# 不符合 Broadcasting 条件\n",
    "try: \n",
    "    tf.broadcast_to(A, [2,32,32,4])\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数学运算\n",
    "\n",
    "### 加、减、乘、除运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=445, shape=(5,), dtype=int32, numpy=array([0, 0, 1, 1, 2])>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = tf.range(5)\n",
    "b = tf.constant(2)\n",
    "# 整除运算\n",
    "a//b "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=446, shape=(5,), dtype=int32, numpy=array([0, 1, 0, 1, 0])>"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 余除运算\n",
    "a%b "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 乘方运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=452, shape=(4,), dtype=int32, numpy=array([ 0,  1,  8, 27])>"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.range(4)\n",
    "# 乘方运算\n",
    "tf.pow(x,3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=454, shape=(4,), dtype=int32, numpy=array([0, 1, 4, 9])>"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 乘方运算符\n",
    "x**2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=457, shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=tf.constant([1.,4.,9.])\n",
    "# 平方根\n",
    "x**(0.5) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = tf.range(5)\n",
    "# 转换为浮点数\n",
    "x = tf.cast(x, dtype=tf.float32) \n",
    "# 平方\n",
    "x = tf.square(x) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=464, shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 平方根\n",
    "tf.sqrt(x) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 指数和对数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=467, shape=(3,), dtype=float32, numpy=array([2., 4., 8.], dtype=float32)>"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.constant([1.,2.,3.])\n",
    "# 指数运算\n",
    "2**x "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=469, shape=(), dtype=float32, numpy=2.7182817>"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自然指数运算\n",
    "tf.exp(1.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=472, shape=(), dtype=float32, numpy=3.0>"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.exp(3.)\n",
    "# 对数运算\n",
    "tf.math.log(x) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=479, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tf.constant([1.,2.])\n",
    "x = 10**x\n",
    "# 换底公式\n",
    "tf.math.log(x)/tf.math.log(10.) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵相乘运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=492, shape=(4, 3, 28, 2), dtype=float32, numpy=\n",
       "array([[[[ 4.52958107e+00, -1.16477842e+01],\n",
       "         [-1.29190836e+01, -8.29940736e-01],\n",
       "         [ 1.38931191e+00,  2.04902387e+00],\n",
       "         [-4.42736387e+00, -1.37812805e+00],\n",
       "         [-2.50435519e+00,  1.75130236e+00],\n",
       "         [-2.62552857e+00,  4.99227095e+00],\n",
       "         [-5.89759445e+00, -3.50469375e+00],\n",
       "         [ 1.55551510e+01,  7.13111639e+00],\n",
       "         [-2.52108812e+00, -5.50317144e+00],\n",
       "         [-4.95809603e+00,  1.08007729e+00],\n",
       "         [ 2.50468284e-01, -1.32984924e+01],\n",
       "         [ 4.04665136e+00,  2.18005919e+00],\n",
       "         [ 3.51398319e-01,  1.38398480e+00],\n",
       "         [-3.13203692e+00, -1.02923460e+01],\n",
       "         [-8.73200703e+00, -7.03380227e-01],\n",
       "         [ 2.00839782e+00,  1.13983507e+01],\n",
       "         [-6.67606258e+00,  4.49518299e+00],\n",
       "         [-1.00654211e+01,  2.70134497e+00],\n",
       "         [ 2.05378866e+00, -5.47033453e+00],\n",
       "         [-5.48454583e-01, -1.33653336e+01],\n",
       "         [ 8.12723637e-01,  7.40620756e+00],\n",
       "         [-9.01960373e+00,  5.10684729e+00],\n",
       "         [-4.58344364e+00, -7.78342915e+00],\n",
       "         [-1.12854967e+01,  4.89407921e+00],\n",
       "         [-4.48695326e+00, -1.42039061e+01],\n",
       "         [-3.99005365e+00,  2.13551474e+00],\n",
       "         [ 3.54528618e+00, -8.57265115e-01],\n",
       "         [-4.64494610e+00,  5.60817719e+00]],\n",
       "\n",
       "        [[-3.77456951e+00,  2.93642569e+00],\n",
       "         [-1.01547642e+01,  3.57945609e+00],\n",
       "         [-1.26865873e+01, -9.72377968e+00],\n",
       "         [ 9.87906933e-01,  1.32380700e+00],\n",
       "         [ 8.11753750e+00,  1.25335944e+00],\n",
       "         [ 1.89922047e+01, -2.16593719e+00],\n",
       "         [ 5.34943151e+00,  4.76214647e+00],\n",
       "         [ 1.97557878e+00, -1.49734163e+00],\n",
       "         [ 1.36943359e+01,  2.60455084e+00],\n",
       "         [ 1.02942810e+01,  5.24122190e+00],\n",
       "         [-7.11409283e+00,  2.14728808e+00],\n",
       "         [ 4.81354856e+00,  2.86331344e+00],\n",
       "         [ 2.27337050e+00, -4.28266287e+00],\n",
       "         [-8.03707027e+00,  1.24369020e+01],\n",
       "         [ 6.81360662e-01,  1.96078146e+00],\n",
       "         [ 3.44457054e+00,  3.47660446e+00],\n",
       "         [ 3.85738701e-01, -1.25663984e+00],\n",
       "         [ 3.15593004e-01,  1.76124060e+00],\n",
       "         [ 1.11583681e+01,  7.84227228e+00],\n",
       "         [-4.01495314e+00, -7.19392824e+00],\n",
       "         [ 1.07859623e+00,  3.06572700e+00],\n",
       "         [ 6.84271431e+00, -4.78523207e+00],\n",
       "         [ 1.15147457e+01,  7.41524982e+00],\n",
       "         [-5.73353672e+00,  1.13922191e+00],\n",
       "         [ 1.92334652e+00,  5.92522717e+00],\n",
       "         [-4.84250546e+00,  9.20046568e-01],\n",
       "         [-7.26901674e+00, -5.88635015e+00],\n",
       "         [-2.61056280e+00, -4.52376187e-01]],\n",
       "\n",
       "        [[-7.52912712e+00, -6.48758316e+00],\n",
       "         [-6.55934048e+00, -4.15966177e+00],\n",
       "         [ 4.57156181e+00,  2.99886584e+00],\n",
       "         [-6.03435755e+00,  1.75634134e+00],\n",
       "         [ 4.60942745e+00,  1.67769980e+00],\n",
       "         [ 1.89002931e+00, -2.03830385e+00],\n",
       "         [ 4.97081375e+00, -8.84398651e+00],\n",
       "         [ 1.52578650e+01,  1.37175322e+01],\n",
       "         [-2.65032887e+00,  8.68390560e-01],\n",
       "         [ 9.67476428e-01,  4.30196714e+00],\n",
       "         [ 4.70789623e+00, -9.35138798e+00],\n",
       "         [-5.15081453e+00,  2.93569398e+00],\n",
       "         [-4.66080856e+00,  8.85905147e-01],\n",
       "         [-4.37495756e+00, -2.10319939e+01],\n",
       "         [-7.18950570e-01,  1.77502525e+00],\n",
       "         [-7.39487028e+00, -8.61856270e+00],\n",
       "         [-8.64685726e+00, -5.65718412e-01],\n",
       "         [ 2.68145657e+00, -1.83125579e+00],\n",
       "         [-5.49992847e+00,  2.26523495e+00],\n",
       "         [ 6.43575573e+00, -6.26262140e+00],\n",
       "         [-9.15338707e+00,  6.52617216e+00],\n",
       "         [-6.24976206e+00, -7.86522269e-01],\n",
       "         [ 1.74770272e+00, -6.28428936e-01],\n",
       "         [ 4.32175279e-01,  2.20229983e+00],\n",
       "         [ 8.35163307e+00,  6.02595985e-01],\n",
       "         [-1.38349843e+00, -4.46900129e+00],\n",
       "         [-5.38275957e+00,  1.37891712e+01],\n",
       "         [-1.41031494e+01,  1.89549983e+00]]],\n",
       "\n",
       "\n",
       "       [[[-1.71823549e+00,  5.69819689e+00],\n",
       "         [ 1.75204563e+00, -2.42419815e+00],\n",
       "         [ 9.60254002e+00, -7.00883055e+00],\n",
       "         [ 1.40529537e+00, -1.09758358e+01],\n",
       "         [-3.56892323e+00, -4.78711843e+00],\n",
       "         [-2.11716700e+00,  1.07452230e+01],\n",
       "         [ 4.97787142e+00, -6.14218414e-01],\n",
       "         [ 8.15732574e+00,  6.08128846e-01],\n",
       "         [-4.33545494e+00,  2.32146716e+00],\n",
       "         [-9.40463603e-01, -5.05069065e+00],\n",
       "         [-1.58624184e+00, -1.61568880e+00],\n",
       "         [ 2.66409576e-01, -1.23528707e+00],\n",
       "         [-5.69994497e+00, -5.58211148e-01],\n",
       "         [-1.79814950e-01,  4.95119619e+00],\n",
       "         [-4.86255455e+00, -3.68646717e+00],\n",
       "         [ 3.91216159e+00,  2.20114064e+00],\n",
       "         [-3.85598779e-01,  3.30422789e-01],\n",
       "         [ 5.13641787e+00,  3.49522567e+00],\n",
       "         [-3.01872683e+00, -1.10289478e+00],\n",
       "         [ 2.07024693e+00,  3.70624423e+00],\n",
       "         [ 1.36781919e+00, -9.99948311e+00],\n",
       "         [-4.52749109e+00,  2.87602711e+00],\n",
       "         [-1.04002399e+01,  5.75715351e+00],\n",
       "         [-4.63330603e+00, -7.24155235e+00],\n",
       "         [ 2.62130737e+00, -1.00201883e+01],\n",
       "         [ 8.42736244e+00,  2.69262362e+00],\n",
       "         [-1.20469275e+01,  1.13278637e+01],\n",
       "         [-8.47890258e-01, -2.89477730e+00]],\n",
       "\n",
       "        [[ 7.18488503e+00, -4.26654196e+00],\n",
       "         [ 5.79737473e+00,  1.85054034e-01],\n",
       "         [-2.59494019e+00, -1.46037772e-01],\n",
       "         [-1.10016501e+00,  1.05683060e+01],\n",
       "         [ 8.15915585e+00,  5.12178278e+00],\n",
       "         [ 2.53910995e+00, -2.72189021e+00],\n",
       "         [ 9.15002155e+00, -7.09629440e+00],\n",
       "         [ 1.95800138e+00,  6.24456227e-01],\n",
       "         [ 1.84685692e-01,  6.85365438e+00],\n",
       "         [ 1.83937669e+00,  2.48948574e+00],\n",
       "         [-1.95469296e+00, -7.74851418e+00],\n",
       "         [-4.52053118e+00,  7.31982231e+00],\n",
       "         [-1.08011112e+01, -2.01057506e+00],\n",
       "         [-1.05694759e+00,  1.86278999e+00],\n",
       "         [ 3.90777469e+00,  2.92098069e+00],\n",
       "         [ 7.42899239e-01,  9.88324547e+00],\n",
       "         [-3.02708054e+00,  3.05603832e-01],\n",
       "         [ 7.79582500e+00, -3.85757864e-01],\n",
       "         [ 8.49275875e+00, -3.06210184e+00],\n",
       "         [ 1.05065155e+01, -8.25929165e+00],\n",
       "         [-6.59509754e+00,  3.56109649e-01],\n",
       "         [-3.13428074e-01,  4.67339277e-01],\n",
       "         [ 2.06375241e+00, -3.29354453e+00],\n",
       "         [ 8.41706181e+00,  8.25147343e+00],\n",
       "         [-3.83852720e+00, -3.16773832e-01],\n",
       "         [-9.02178574e+00, -6.81530380e+00],\n",
       "         [-2.12502956e+00, -7.59165621e+00],\n",
       "         [ 3.03440571e+00, -5.00925779e-01]],\n",
       "\n",
       "        [[-4.26394272e+00,  4.65443563e+00],\n",
       "         [ 1.64005089e+00,  4.61360550e+00],\n",
       "         [ 2.31111914e-01, -1.58527100e+00],\n",
       "         [ 5.59088993e+00,  1.70225108e+00],\n",
       "         [ 6.05089474e+00,  7.70087957e+00],\n",
       "         [-3.39722753e+00,  1.29221992e+01],\n",
       "         [-1.25925934e+00,  8.52115345e+00],\n",
       "         [-5.44128132e+00, -1.31848681e+00],\n",
       "         [ 2.78428841e+00, -1.23615742e+00],\n",
       "         [ 4.77663517e+00,  8.84560299e+00],\n",
       "         [ 5.24873590e+00,  1.20781155e+01],\n",
       "         [-3.47308397e+00,  1.90415573e+00],\n",
       "         [-4.82842731e+00,  7.45103550e+00],\n",
       "         [-8.51837158e+00, -1.43130722e+01],\n",
       "         [-3.41000891e+00,  6.43959379e+00],\n",
       "         [ 4.60882902e-01, -8.26375961e+00],\n",
       "         [-4.03726387e+00,  2.65284562e+00],\n",
       "         [ 9.83798061e-04, -7.73873615e+00],\n",
       "         [-1.34317532e+01, -6.44769573e+00],\n",
       "         [-4.51945066e+00,  4.01860285e+00],\n",
       "         [-6.92380428e+00, -7.57486057e+00],\n",
       "         [ 7.41759002e-01, -1.54575605e+01],\n",
       "         [-5.78783321e+00, -4.83845711e+00],\n",
       "         [-3.22081089e+00, -7.42168307e-01],\n",
       "         [ 2.68032169e+00, -1.23681297e+01],\n",
       "         [-3.79782820e+00, -1.07660806e+00],\n",
       "         [-8.05784106e-01,  3.23638940e+00],\n",
       "         [ 4.20486736e+00, -7.09753895e+00]]],\n",
       "\n",
       "\n",
       "       [[[-4.04214621e+00, -9.07598591e+00],\n",
       "         [ 5.19152939e-01,  3.88124561e+00],\n",
       "         [ 1.84847188e+00, -5.87157679e+00],\n",
       "         [ 4.61582661e+00,  1.23423853e+01],\n",
       "         [ 4.54298019e+00,  1.06949644e+01],\n",
       "         [ 3.87103748e+00, -3.31618881e+00],\n",
       "         [ 2.42885756e+00, -6.59725952e+00],\n",
       "         [ 8.63337517e+00,  3.06053638e+00],\n",
       "         [-1.14390011e+01, -5.12284756e+00],\n",
       "         [-8.25125027e+00,  2.14935160e+00],\n",
       "         [ 2.39876437e+00,  3.97758746e+00],\n",
       "         [-3.01949406e+00,  8.13371718e-01],\n",
       "         [-1.46735430e+00, -3.55673885e+00],\n",
       "         [-2.36041956e-02, -2.22603059e+00],\n",
       "         [ 3.43662095e+00, -5.52456200e-01],\n",
       "         [-5.58895063e+00,  5.49133569e-02],\n",
       "         [ 9.61740434e-01, -1.52028930e+00],\n",
       "         [ 4.82709312e+00,  4.44803238e+00],\n",
       "         [-7.36245060e+00, -8.51479435e+00],\n",
       "         [-8.36224556e+00, -4.99080706e+00],\n",
       "         [ 3.33578563e+00, -1.07970858e+01],\n",
       "         [-2.77002406e+00,  5.17520618e+00],\n",
       "         [ 1.16014023e+01,  7.06166267e-01],\n",
       "         [ 4.04642582e+00,  9.48558092e-01],\n",
       "         [-5.45400238e+00,  3.92564446e-01],\n",
       "         [ 1.02622929e+01, -9.41519678e-01],\n",
       "         [ 8.00186920e+00,  7.05086756e+00],\n",
       "         [ 3.59276104e+00,  1.12449443e+00]],\n",
       "\n",
       "        [[ 2.48934174e+00,  1.72585285e+00],\n",
       "         [-2.26483727e+00,  3.85691595e+00],\n",
       "         [-3.61736178e+00,  1.91254663e+00],\n",
       "         [ 6.42045498e-01,  1.00995755e+00],\n",
       "         [ 3.41186619e+00,  5.87120342e+00],\n",
       "         [ 6.21719122e-01, -1.15074202e-01],\n",
       "         [ 2.20202875e+00, -3.33168912e+00],\n",
       "         [-1.06967103e+00, -1.41958523e+00],\n",
       "         [-1.90975170e+01,  4.72794104e+00],\n",
       "         [-3.98611879e+00,  3.67467523e+00],\n",
       "         [ 6.29600048e+00, -6.02819967e+00],\n",
       "         [-7.90012419e-01, -3.21522999e+00],\n",
       "         [-1.21613560e+01,  9.17352009e+00],\n",
       "         [-5.90637684e+00,  2.65815592e+00],\n",
       "         [ 6.76181746e+00, -8.32247972e-01],\n",
       "         [-8.43671513e+00,  7.32634020e+00],\n",
       "         [ 7.12821186e-01,  1.54911041e+00],\n",
       "         [ 1.30253248e+01, -1.36551018e+01],\n",
       "         [-2.80966163e+00,  3.01643610e+00],\n",
       "         [ 4.40880299e+00, -9.70179260e-01],\n",
       "         [ 3.06368995e+00, -7.17901564e+00],\n",
       "         [-7.97105265e+00,  1.86493456e+00],\n",
       "         [ 5.69449615e+00, -8.28563881e+00],\n",
       "         [-1.47611880e+00,  3.88842583e+00],\n",
       "         [ 9.57997131e+00, -5.01015365e-01],\n",
       "         [-4.53809071e+00, -2.75313830e+00],\n",
       "         [ 1.30926621e+00,  1.87483644e+00],\n",
       "         [ 2.18864346e+00, -4.50913620e+00]],\n",
       "\n",
       "        [[-1.37736273e+00,  6.97940171e-01],\n",
       "         [-1.22325382e+01, -4.29633093e+00],\n",
       "         [-4.63244867e+00,  3.98127794e+00],\n",
       "         [-2.23171377e+00, -3.56233430e+00],\n",
       "         [ 7.88202333e+00, -1.15235329e+00],\n",
       "         [-9.47351837e+00, -5.06309652e+00],\n",
       "         [ 5.09796441e-01,  5.96302271e+00],\n",
       "         [-3.19092941e+00,  2.32346272e+00],\n",
       "         [-9.53168929e-01, -1.62822223e+00],\n",
       "         [-2.69748902e+00, -5.30024195e+00],\n",
       "         [ 2.89734411e+00,  9.87599564e+00],\n",
       "         [-3.88054800e+00,  3.20985973e-01],\n",
       "         [-2.63242078e+00, -1.25956039e+01],\n",
       "         [ 1.71013832e+00,  6.65569687e+00],\n",
       "         [-1.01352386e+01, -2.81450838e-01],\n",
       "         [-7.49713659e+00, -5.07849073e+00],\n",
       "         [-7.12377024e+00, -2.70455456e+00],\n",
       "         [ 5.56181765e+00,  3.09287643e+00],\n",
       "         [ 1.33259892e+00, -7.84151888e+00],\n",
       "         [-6.63341713e+00,  1.03914750e+00],\n",
       "         [-4.47664452e+00,  1.95807850e+00],\n",
       "         [-7.10351801e+00,  4.28743076e+00],\n",
       "         [-8.57909012e+00, -1.08757496e+00],\n",
       "         [-2.63936543e+00,  6.17916298e+00],\n",
       "         [ 3.14372134e+00, -6.17811978e-01],\n",
       "         [ 3.98816442e+00,  9.57448578e+00],\n",
       "         [ 4.01238155e+00,  7.84229994e-01],\n",
       "         [-3.14543653e+00, -8.43471050e+00]]],\n",
       "\n",
       "\n",
       "       [[[-9.14662075e+00,  4.69213343e+00],\n",
       "         [-4.42761660e+00, -4.00750637e+00],\n",
       "         [-5.96911144e+00,  3.12246060e+00],\n",
       "         [-6.77465248e+00, -8.41268635e+00],\n",
       "         [-1.39320254e+00,  4.44465685e+00],\n",
       "         [-7.57896185e+00,  5.53610563e+00],\n",
       "         [ 8.30130816e-01,  4.70735073e+00],\n",
       "         [-1.87982922e+01,  4.20530987e+00],\n",
       "         [ 5.79305935e+00,  9.69469428e-01],\n",
       "         [ 3.83739853e+00,  5.12833416e-01],\n",
       "         [-1.03213749e+01,  5.24390221e+00],\n",
       "         [ 1.45444250e+01, -6.68088627e+00],\n",
       "         [-1.16166773e+01,  9.43345737e+00],\n",
       "         [ 1.06194830e+01, -4.47001600e+00],\n",
       "         [ 3.88577175e+00, -3.72340131e+00],\n",
       "         [ 4.58559656e+00, -2.58050084e+00],\n",
       "         [ 8.41269016e+00,  6.05459139e-02],\n",
       "         [-1.12093544e+01,  4.07041216e+00],\n",
       "         [ 9.77533531e+00, -5.99373722e+00],\n",
       "         [ 4.81003618e+00, -3.88377857e+00],\n",
       "         [-7.63114595e+00,  4.24975395e+00],\n",
       "         [-4.80876178e-01, -2.22696826e-01],\n",
       "         [ 9.54653168e+00, -8.47588825e+00],\n",
       "         [ 5.12853336e+00,  2.56165791e+00],\n",
       "         [-4.04553652e+00,  7.47251570e-01],\n",
       "         [-2.83544564e+00,  5.52741241e+00],\n",
       "         [ 2.96107173e+00,  8.45366716e-01],\n",
       "         [ 2.44998956e+00,  4.90139097e-01]],\n",
       "\n",
       "        [[ 1.51958776e+00,  1.10383511e+00],\n",
       "         [-1.42577419e+01,  1.69874954e+00],\n",
       "         [-9.19488430e+00,  1.09138126e+01],\n",
       "         [ 6.64786863e+00, -1.84604034e-01],\n",
       "         [ 1.52249441e+01, -3.55621791e+00],\n",
       "         [ 3.37584829e+00, -3.97526717e+00],\n",
       "         [ 6.46889257e+00,  2.85531306e+00],\n",
       "         [-6.51854420e+00, -3.31579256e+00],\n",
       "         [ 5.14864540e+00,  1.45266938e+00],\n",
       "         [ 5.07186890e+00,  1.87256062e+00],\n",
       "         [ 1.63114250e+00,  6.27998495e+00],\n",
       "         [ 1.15979099e+01, -7.85989571e+00],\n",
       "         [ 8.97180557e+00, -9.58922803e-01],\n",
       "         [ 4.67463398e+00,  1.40679932e+00],\n",
       "         [ 4.35190821e+00, -1.40829349e+00],\n",
       "         [ 1.16467273e+00,  5.69324903e-02],\n",
       "         [ 3.60505581e+00, -5.78942060e+00],\n",
       "         [-1.37210524e+00,  4.57523298e+00],\n",
       "         [-5.58913183e+00,  4.14475536e+00],\n",
       "         [ 1.82180357e+00,  1.84028077e+00],\n",
       "         [-4.09431601e+00,  3.54199791e+00],\n",
       "         [-1.09562197e+01,  5.83333397e+00],\n",
       "         [ 3.85553384e+00, -1.66377914e+00],\n",
       "         [-5.23692179e+00,  6.04868650e+00],\n",
       "         [-1.09517765e+01, -3.16304541e+00],\n",
       "         [-2.11847925e+00, -5.08617449e+00],\n",
       "         [-4.27438879e+00, -2.50236845e+00],\n",
       "         [ 3.52000952e+00,  2.39096856e+00]],\n",
       "\n",
       "        [[ 4.90632832e-01,  9.02468967e+00],\n",
       "         [ 8.27245295e-01, -2.62228036e+00],\n",
       "         [-4.64681005e+00, -1.64752394e-01],\n",
       "         [-6.88722467e+00,  1.01598489e+00],\n",
       "         [-5.80170584e+00,  8.08591652e+00],\n",
       "         [ 3.56913060e-01,  3.07487190e-01],\n",
       "         [-4.21453762e+00,  1.29255543e+01],\n",
       "         [-5.31807518e+00,  3.99313521e+00],\n",
       "         [-1.22184622e+00,  2.82342076e+00],\n",
       "         [-8.80433083e-01,  4.41309643e+00],\n",
       "         [ 1.01031490e-01, -1.61932278e+01],\n",
       "         [ 3.32862568e+00, -8.88586044e+00],\n",
       "         [-8.63054085e+00, -3.89329982e+00],\n",
       "         [ 1.10366516e+01, -6.71763754e+00],\n",
       "         [ 3.08323216e+00, -3.85804319e+00],\n",
       "         [-2.83281898e+00,  8.49434257e-01],\n",
       "         [ 1.11698878e+00,  4.33968163e+00],\n",
       "         [ 6.51928997e+00, -6.99774742e-01],\n",
       "         [-4.72331381e+00, -4.88590097e+00],\n",
       "         [ 8.14846456e-01, -1.98010230e+00],\n",
       "         [-5.24323797e+00,  1.52109127e+01],\n",
       "         [-7.78682604e-02,  1.80287945e+00],\n",
       "         [-8.00994873e+00, -4.76525640e+00],\n",
       "         [-6.22093773e+00,  1.16642141e+00],\n",
       "         [-1.97488809e+00,  8.21198654e+00],\n",
       "         [ 3.01348019e+00,  1.11014378e+00],\n",
       "         [-2.32281399e+00,  5.74964094e+00],\n",
       "         [ 1.21908295e+00,  3.58456659e+00]]]], dtype=float32)>"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = tf.random.normal([4,3,28,32])\n",
    "b = tf.random.normal([4,3,32,2])\n",
    "# 批量形式的矩阵相乘\n",
    "a@b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=505, shape=(4, 28, 16), dtype=float32, numpy=\n",
       "array([[[ -4.193048  ,  -4.7306323 ,   1.8329823 , ...,  -0.25694013,\n",
       "           5.7625575 , -11.154146  ],\n",
       "        [ -4.181064  ,  10.8917465 ,  -6.153747  , ...,  -6.0570545 ,\n",
       "         -10.627351  ,  -2.8575366 ],\n",
       "        [ -1.1189293 ,   4.3246474 , -13.187651  , ...,   0.94277   ,\n",
       "          -4.860285  ,  -8.34903   ],\n",
       "        ...,\n",
       "        [  5.979231  ,   0.5809582 ,   6.5631504 , ...,   1.1640375 ,\n",
       "          -8.018835  ,   3.316239  ],\n",
       "        [ -3.4417086 ,   0.27663276,  -0.73314005, ...,   1.3875468 ,\n",
       "           1.3992321 ,   4.5397024 ],\n",
       "        [  2.2438467 ,   2.0971882 ,   3.871925  , ...,   1.6502293 ,\n",
       "           2.1999664 ,   5.350239  ]],\n",
       "\n",
       "       [[ -3.3425956 ,  -2.0814571 ,  -0.2907685 , ...,  -2.8967288 ,\n",
       "          -7.4164124 ,   3.1385334 ],\n",
       "        [ -6.2438693 ,  -5.4578714 ,  -7.125825  , ...,  -0.5904703 ,\n",
       "          -0.46187064,   0.2875834 ],\n",
       "        [  1.842198  ,   0.8709512 ,  -4.587543  , ...,  -8.552272  ,\n",
       "           1.2899829 ,  -4.31886   ],\n",
       "        ...,\n",
       "        [-11.144563  ,  -1.0086232 ,   6.460386  , ...,  -0.30220097,\n",
       "           8.735367  ,   1.3449    ],\n",
       "        [  0.41578403,  11.829522  ,   1.8000128 , ...,  -0.8147756 ,\n",
       "           3.149985  ,  -1.7904522 ],\n",
       "        [  0.04661513,   0.34791204,  -2.4636276 , ...,   1.9672587 ,\n",
       "           2.8629146 ,  -1.0756897 ]],\n",
       "\n",
       "       [[  5.0635    ,  -3.4273264 ,  -0.5836348 , ...,  -3.3027453 ,\n",
       "           7.138449  ,   4.768684  ],\n",
       "        [ -5.291548  ,   2.1069334 ,   0.5836308 , ...,  -1.343643  ,\n",
       "           5.2932925 ,  -3.2054362 ],\n",
       "        [ -6.264811  ,  -1.6770005 ,   0.95482373, ...,   2.4965386 ,\n",
       "           1.4676853 ,  -1.5350066 ],\n",
       "        ...,\n",
       "        [  0.495609  ,  -6.7183814 ,  -2.6808412 , ...,  -0.18018769,\n",
       "           0.43428445,  -7.482387  ],\n",
       "        [ -0.5499466 ,   3.5323002 ,   9.761492  , ...,   1.902858  ,\n",
       "         -10.6918335 ,  -3.735208  ],\n",
       "        [  0.9746555 ,   5.566572  ,   4.312503  , ...,  -0.1628262 ,\n",
       "          -2.549673  ,   4.828361  ]],\n",
       "\n",
       "       [[ -2.0312853 ,  -4.243564  , -10.121379  , ...,  -8.935034  ,\n",
       "          -2.2999423 ,   2.9266613 ],\n",
       "        [ -2.9180217 ,  -2.6550953 ,  -0.16884886, ...,  -2.1124852 ,\n",
       "           7.8735843 ,  -9.592941  ],\n",
       "        [ 13.970577  ,   2.040014  ,   6.656541  , ...,   2.0156534 ,\n",
       "           1.8864409 ,   4.774583  ],\n",
       "        ...,\n",
       "        [  1.0177426 ,   1.5944879 ,  -2.7742393 , ...,  -4.1962085 ,\n",
       "          -7.683416  ,   3.542084  ],\n",
       "        [ -0.5650038 ,  -7.2887745 ,   5.8197746 , ...,   0.3233802 ,\n",
       "          -6.4272513 ,  -1.0968776 ],\n",
       "        [ -5.581814  ,   6.4620285 ,  -4.612803  , ...,  -0.24331895,\n",
       "           1.6834859 ,  -1.8596684 ]]], dtype=float32)>"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = tf.random.normal([4,28,32])\n",
    "b = tf.random.normal([32,16])\n",
    "# 先自动扩展，再矩阵相乘\n",
    "tf.matmul(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 前向传播实战"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import tensorflow as tf\n",
    "import tensorflow.keras.datasets as datasets\n",
    "\n",
    "plt.rcParams['font.size'] = 16\n",
    "plt.rcParams['font.family'] = ['STKaiti']\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data():\n",
    "    # 加载 MNIST 数据集\n",
    "    (x, y), (x_val, y_val) = datasets.mnist.load_data()\n",
    "    # 转换为浮点张量， 并缩放到-1~1\n",
    "    x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.\n",
    "    # 转换为整形张量\n",
    "    y = tf.convert_to_tensor(y, dtype=tf.int32)\n",
    "    # one-hot 编码\n",
    "    y = tf.one_hot(y, depth=10)\n",
    "\n",
    "    # 改变视图， [b, 28, 28] => [b, 28*28]\n",
    "    x = tf.reshape(x, (-1, 28 * 28))\n",
    "\n",
    "    # 构建数据集对象\n",
    "    train_dataset = tf.data.Dataset.from_tensor_slices((x, y))\n",
    "    # 批量训练\n",
    "    train_dataset = train_dataset.batch(200)\n",
    "    return train_dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "def init_paramaters():\n",
    "    # 每层的张量都需要被优化，故使用 Variable 类型，并使用截断的正太分布初始化权值张量\n",
    "    # 偏置向量初始化为 0 即可\n",
    "    # 第一层的参数\n",
    "    w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))\n",
    "    b1 = tf.Variable(tf.zeros([256]))\n",
    "    # 第二层的参数\n",
    "    w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))\n",
    "    b2 = tf.Variable(tf.zeros([128]))\n",
    "    # 第三层的参数\n",
    "    w3 = tf.Variable(tf.random.truncated_normal([128, 10], stddev=0.1))\n",
    "    b3 = tf.Variable(tf.zeros([10]))\n",
    "    return w1, b1, w2, b2, w3, b3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_epoch(epoch, train_dataset, w1, b1, w2, b2, w3, b3, lr=0.001):\n",
    "    for step, (x, y) in enumerate(train_dataset):\n",
    "        with tf.GradientTape() as tape:\n",
    "            # 第一层计算， [b, 784]@[784, 256] + [256] => [b, 256] + [256] => [b,256] + [b, 256]\n",
    "            h1 = x @ w1 + tf.broadcast_to(b1, (x.shape[0], 256))\n",
    "            h1 = tf.nn.relu(h1)  # 通过激活函数\n",
    "\n",
    "            # 第二层计算， [b, 256] => [b, 128]\n",
    "            h2 = h1 @ w2 + b2\n",
    "            h2 = tf.nn.relu(h2)\n",
    "            # 输出层计算， [b, 128] => [b, 10]\n",
    "            out = h2 @ w3 + b3\n",
    "\n",
    "            # 计算网络输出与标签之间的均方差， mse = mean(sum(y-out)^2)\n",
    "            # [b, 10]\n",
    "            loss = tf.square(y - out)\n",
    "            # 误差标量， mean: scalar\n",
    "            loss = tf.reduce_mean(loss)\n",
    "\n",
    "            # 自动梯度，需要求梯度的张量有[w1, b1, w2, b2, w3, b3]\n",
    "            grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])\n",
    "\n",
    "        # 梯度更新， assign_sub 将当前值减去参数值，原地更新\n",
    "        w1.assign_sub(lr * grads[0])\n",
    "        b1.assign_sub(lr * grads[1])\n",
    "        w2.assign_sub(lr * grads[2])\n",
    "        b2.assign_sub(lr * grads[3])\n",
    "        w3.assign_sub(lr * grads[4])\n",
    "        b3.assign_sub(lr * grads[5])    \n",
    "    \n",
    "    return loss.numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(epochs):\n",
    "    losses = []\n",
    "    train_dataset = load_data()\n",
    "    w1, b1, w2, b2, w3, b3 = init_paramaters()\n",
    "    for epoch in range(epochs):\n",
    "        loss = train_epoch(epoch, train_dataset, w1, b1, w2, b2, w3, b3, lr=0.001)\n",
    "        print('epoch:', epoch, 'loss:', loss)\n",
    "        losses.append(loss)\n",
    "\n",
    "    x = [i for i in range(0, epochs)]\n",
    "    # 绘制曲线\n",
    "    plt.plot(x, losses, color='blue', marker='s', label='训练')\n",
    "    plt.xlabel('Epoch')\n",
    "    plt.ylabel('MSE')\n",
    "    plt.legend()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 0 loss: 0.16430502\n",
      "epoch: 1 loss: 0.1473901\n",
      "epoch: 2 loss: 0.13449846\n",
      "epoch: 3 loss: 0.12467159\n",
      "epoch: 4 loss: 0.11701874\n",
      "epoch: 5 loss: 0.110820025\n",
      "epoch: 6 loss: 0.105716765\n",
      "epoch: 7 loss: 0.10139298\n",
      "epoch: 8 loss: 0.09769247\n",
      "epoch: 9 loss: 0.09452056\n",
      "epoch: 10 loss: 0.09176841\n",
      "epoch: 11 loss: 0.08933158\n",
      "epoch: 12 loss: 0.08714955\n",
      "epoch: 13 loss: 0.08515776\n",
      "epoch: 14 loss: 0.08333652\n",
      "epoch: 15 loss: 0.08166271\n",
      "epoch: 16 loss: 0.08011957\n",
      "epoch: 17 loss: 0.07869925\n",
      "epoch: 18 loss: 0.07738015\n",
      "epoch: 19 loss: 0.07615029\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZEAAAEQCAYAAABxzUkqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3deXhU5fn/8fedEHZBtkBFUXFDRQVBxQ2xVotCVXADtRVoQTZFsIhV8Vu16tcdURaxWLAKSmur1qqUuvzUWsFAXeuXqohVZAkCKnuA+/fHmcAwTibJZGbOzOTzuq5zJfOcJ+fcM8bcPOsxd0dERCQZBWEHICIiuUtJREREkqYkIiIiSVMSERGRpCmJiIhI0uqEHUAmtWzZ0vfbb7+wwxARySkLFy5c7e6t4p2rVUlkv/32o6SkJOwwRERyipl9XtE5dWeJiEjSlERERCRpSiIiIpI0JREREUmakoiIiCRNSSSBNm3A7PtHmzZhRyYikh1q1RTf6lq5snrlIpJ6mzdvprS0lM2bN7Nt27aww8krRUVFFBcX06RJk6SvoSQiIlnrm2++YeXKlbRq1Yo2bdpQp04dzCzssPKCu7Np0yaWLVsGkHQiUXeWiGSt1atXs/fee9OsWTOKioqUQFLIzGjYsCFt27Zl1apVSV9HSUREstbWrVtp0KBB2GHktQYNGlBWVpb0zyuJiEhWU+sjvWr6+SqJJNC6dfXKRURqm7xIImZWbGbHpfq6K1aA+65j0CBo2BA++ijVdxIRyU0ZTyIWGGZmQ81slJkdnaBufTOba2Y9KjhfaGY3ApcD76cp5J1GjoSNG2HGjHTfSUQkN4TREhkFvO3uU939fmCwmTWNrWRmewL3AwfGu4iZ1QH+DHzu7re4+8Z0Bg3QuTOceCJMmgQ7dqT7biKS79avX5/w/KBBg5g2bVrccy+99BInn3wy33777W7lQ4YM4fbbb09ZjJXJaBIxs0Kgn7tHP9RjPjAgTvUzgTHAFxVc7nZgpbvPTGmQlRg5Ej79FObOzeRdRSQVsmkXiu3btzNo0CA6dOjAt99+S2lp6ffqHHzwwXzwwQdxf37hwoV07tyZzZs371a+dOlS+vTpk5aY48l0S6Q7sDmmbDHQP7aiu8929w3xLmJmBwFDgRtSHmEl+vYNfuEefDDTdxaRmsqmXSgKCwuZM2cO48ePZ8uWLcyZM4fRo0ezYcMG5syZA0D9+vU54ogjvvezZWVlfPbZZ0yYMIERI0Ywa9YsduzYwYIFC1i+fDk33ngj7dq1o0OHDmzdujWt7yPTK9bbA2tiytZEyqvj58CLwFYzGwr0Aka5+5Kah5hY3bpw+eVw883wySdwYNzONhFJp6uugnfeSe01e/SoXv1OnWDChJrdc8uWLVxyySUAdOnShZKSEho2bMjo0aM577zzKCgoiLtOZvr06VxzzTUsWLCADRs20LNnT2bNmsXEiRPp3r07Xbp0YcyYMRx11FHUrVu3ZkFWItMtkWIgdlVLGdDSzKoTy0nA/3P3te4+FZgF/DZeRTMbYmYlZlYSr7mYjCFDoLAQpkxJyeVEpJb573//y6RJkyguLmbAgAEA7LHHHju3dTn22GMpLCzEzL63jmPq1KmMGTOGvn37Mm7cOJ566imaN29O//79ady4Md26daO0tJRu3bplZKFmplsiq4DYtFgErHb36gxVt2L3sZJ5wCwzaxTbBebu04BpAF27dvXqh/x9e+0F550HjzwStEgaNUrFVUWkqpJtASRaV/fqq8lds7qee+45HnjgAS677DJOO+00hg8fHonNqFMn+JNc/sc/XhK54IILuPjii3nhhRc4/fTTadCgAevWreOKK65g6dKlDBw4kIEDB2bmzZD5lsgSggQQrRnwWTWvswaoF/W6fHpCiyTjqraRI2HdOnj88UzdUUTyQe/evZk7dy4XX3wx27Zto2PHjgBs3LiRPfbYA4CCguBPs7vv/L5c8+bNGTduHFOnTuXKK69k6tSpANStW5exY8dy4YUXsmjRIk444QRatWrFlDR3mWS6JfI60MTMCqJaHocAs6t5nQ8iP1euBbAV+KrmIVbNiScGfaIPPgiDByf+F46IZIfWreMPooe1C0VZWRn16tXjySefpH79+uy9994AO7e83xFnLYGZ0bhxY+68806OOeYYAG6++WYOOOAAjjzySPbff38+/PBDrr766oy8h4y2RNx9GzAd6BlV3A2YYWbnm1lVp+s+APSPTBkGOA14OHL9jDALWiPvvw+vv56pu4pITcTuQlF+rFiR+Vh27NjBl19+Sb9+/TjyyCN57bXXOPbYY9m4cSPLly8HqPD5KXfddRdt2rRh4sSJ3HXXXYwfP56ePXty4okn0rx5c7Zs2cK//vUvFi9enPb3EcZiw4nAwWY23MxGAdPdfR3QFuhYXsnMGpjZEKATMNTMdp5z9/eA24AJZnYl0BUYl8k3AdC/PzRrpum+IlJ9Dz/8MIsXL2bEiBG0aNGCd999d+dgeHkLY9OmTbtN0V21ahXXX389xx9/PBdddBGHHXYYY8eO5dlnn+XRRx8Fgm6xzZs30759e3r16sWkSZPS+j4y/lAqd3fge8NikdXr90e93kQwIB53uaa7PwY8lqYwq6RhQ/j5z+G++2DZMmjbNsxoRCSXNG7cmLvvvpvu3bszaNAgJk+eDATdVXfffTcAy5Yto3nz5jt/pkWLFqxbt47Ro0dz4YUX7iy/9dZbeeihh9ixYwcrV65k/fr1NG3alCeeeIJBgwYxYsSItL2PvNiAMUzDhgVboDz0UNiRiEguueSSS7jyyitZuHAht956KwcffPD36ixdunS3cZHCwkImTZq0WwIBeOihh+jcuTO33HILQ4YM2fmUwq5duzJv3ry0vg8lkRpq3x569QqSyJYtYUcjIrnmmGOOoW0F3Rg/+tGPOOSQQ+Kei9a5c2cArrvuOg499NCdCxgBWqd51oCesZ4CI0dCz57wxz9C1H87EZEaqe4Mq6KiIt566600RROfWiIpcPrpcNBBGmAXkdpHSSQFCgqC1shbb0FJSeX1RUTyhZJIilx2WbD9SZpn04mIZBUlkRRp2hR+9jOYPRtWrw47GpH8EawKkHSp6eerJJJCI0YEM7SmTw87EpH8ULduXTZt2hR2GHlt06ZNFBUVJf3zSiIpdPjhcOqpMHkybN8edjQiua9ly5Z8+eWXrFmzhrKyMrVKUsjd2bhxI8uWLaO4uDjp62iKb4qNHBlsE//cc3DOOWFHI5LbmjZtSr169SgtLeXrr7+ucC8pSU5RURGtW7feuTgxGUoiKXb22bD33vDAA0oiIqlQv3599tlnn7DDkAqoOyvF6tQJtkJ56SX46KOwoxERSS8lkTQYPDh4Frum+4pIvlMSSYNWraBfP5g5E779tvL6IiK5SkkkTUaOhPXrIbLFv4hIXlISSZNjjoFjjw3209KsRBHJV0oiaTRyJCxeHAyyi4jkIyWRNLrggmB85IEHwo5ERCQ9lETSaL/9oLQUnn0WzHYdbdqEHZmISGooiaTRypXVKxcRyTVKIiIikjQlERERSZqSiIiIJE1JREREkqYkkkatW8cvb9Eis3GIiKSLkkgarVgRrFYvP0pLoUkTOP74sCMTEUkNJZEMatkSrrsueGDVyy+HHY2ISM0piWTYlVdCu3bwy1/Cjh1hRyMiUjNKIhnWoAHcdhv8618wa1bY0YiI1IySSAj694cuXYKurU2bwo5GRCR5SiIhKCiAu++GL76A++8POxoRkeQpiYSkRw84++yga6u0NOxoRESSoyQSojvugI0b4aabwo5ERCQ5GU8iFhhmZkPNbJSZHZ2gbn0zm2tmPSq55tMpDzQDOnSAIUNg6tTg4VUiIrkmjJbIKOBtd5/q7vcDg82saWwlM9sTuB84MNHFzOwi4Jy0RJoBv/41NGwI48aFHYmISPVlNImYWSHQz91LoornAwPiVD8TGAN8keB6ragkyWS74mK49lp45hl47bWwoxERqZ5Mt0S6A5tjyhYD/WMruvtsd99QyfVGA/emKLbQXHUV7L23FiCKSO7JdBJpD6yJKVsTKa8WMzsX+Ju75/xKi4YN4dZb4e234cknw45GRKTqMp1EioGymLIyoKWZVTmWyBjK0e7+ahXqDjGzEjMrKc3iubSXXgqdOsGvfgWbY9tqIiJZKtNJZBVQN6asCFjt7tXpyLkWmFiViu4+zd27unvXVq1aVeMWmVVQAPfcA59/Dg88EHY0IiJVk+kksgSI/UveDPisqheIDKb3A/5uZu+Y2TuR8nfMrEqJJVv98Idw1llB19bq1WFHIyJSuUwnkdeBJjFdV4cAs6t6AXcvdff93b1T+REp7+TuV6Y43oy780747ju45ZawIxERqVxGk4i7bwOmAz2jirsBM8zsfDObmcl4stHhh8MvfgGTJ8PHH4cdjYhIYmEsNpwIHGxmw81sFDDd3dcBbYGO5ZXMrIGZDQE6AUPNrGP8y+Wfm26CevWCQXYRkWxm7h52DBnTtWtXLykpqbxiFrjlFrjxRnjjDTjxxLCjEZHazMwWunvXeOe0AWOWGjMG9toLrr46eD67iEg2UhLJUo0awW9+A/Pnwx/+EHY0IiLxKYlksfIxkYsuArNdR5s24cYlIlJOSSSLrVxZvXIRkUxTEhERkaQpiYiISNKUREREJGlKIiIikjQlkSzWunX88sLCYH8tEZGwKYlksRUrgoWG0cerrwZfr7gi7OhERJREcs4pp8ANN8DMmfD442FHIyK1nZJIDho/Hk46CYYNg08/DTsaEanNlERyUJ06QSuksBD694etW8OOSERqKyWRHNWuHfz2t/D228FuvyIiYVASyWHnnQeXXw533AHz5oUdjYjURkoiOe7ee+Gww+BnP4NVq8KORkRqGyWRHNewITzxBKxdCwMGwI4dYUckIrWJkkgeOOKIoEXywgswcWLY0YhIbaIkkieGDYNzzoFrroFFi8KORkRqCyWRPGEG06dDcTH06wfr14cdkYjUBkoieaRFi2D9yCefaFsUEckMJZE8U74tyowZMGtW2NGISL5TEslDN94IJ5wAQ4fCkiVhRyMi+UxJJA/VqRO0QgoKgm1RysrCjkhE8lXSScTMGqYyEEmtffcNvi5YAHXrBgPv5UebNuHGJiL5o8IkYmaVJZjuZvZnM/tnimOSFPnmm/jlK1dmNg4RyV+JEsVaM7vbzPaLd9LdXwTOB9amIS4REckBiZLIWHf/pbsvNbNDzWyMmX1qZteb2aEA7r4dmJuZUEVEJNvUSXBu53I1d/8I+MjMtrr7gzH1NGwrIlJLJWqJWJyyeI8/ildPstymTWFHICL5IFES8Speo6r1JMNat6743IUXauqviNRcou6sPmZWyO4tjePNbHNMvTOBySmPTGpsxYr45VOmwPDhwdbxv/99sJ5ERCQZiZLIeZEj1mUxr9USyTHDhgXPH7n+ethzT3jwwWD9iIhIdSX6N+g4dy+o7AAGVeeGFhhmZkPNbJSZHZ2gbn0zm2tmPeKc62tm75jZSjN71MyaVieO2u5Xv4Jf/hImT4bx48OORkRyVaKWyAtVvMYH1bznKOANdy8BMLMpZnatu++2NM7M9gTuAA6MvYCZdQGOB44B6gF/BCYBl1YzllrLDO68E9atg1tvhWbN4Oqrw45KRHJNhS0Rd4+bHMyshZmdbWbdzazI3RdW9WaRMZZ+5QkkYj4wIE71M4ExwBdxznUHrnH3MndfD/wPcK6ZOmWqwwymToULLghaJdOnhx2RiOSaRNue9DCzyWZ2UlTZUcBi4GHgSuA5M9urGvfrDsQOzC8G+sdWdPfZ7r6hguv81t2jx2LKgE0xZVIFhYXw2GPw4x/DkCHwxz+GHZGI5JJEYyJjgHvc/Q0AM6sDzAK2AEe7+/kEf/yHV+N+7YE1MWVrIuVV5u7fxRT9EHgsXl0zG2JmJWZWUlpaWp3b1Bp168JTT8Hxx8PFF8Nc7UEgIlWUKIksdPdPo16PBA4FrnT3ZQDuvgZYVY37FfP9Fe5lQMsqbPgYl5k1I+j6+p945919mrt3dfeurVq1SuYWtUKjRvDcc3DYYdC3L7z5ZtgRiUguSPSH+9vyb8ysBTAemOvuT8XU27ca91sF1I0pKwJWu/uOalynPC4DfgMMdvdvK6svie25Z9AKadsWzjoL3n037IhEJNslSiIFZnZSJIE8BuwAhkZXiIyRdKvG/ZYAsc2BZsBn1bhGtLHAJHfX8/tSpHVrmDcP9tgDzjgDPv447IhEJJslSiJTgdOBvxMsKDzN3T8HMLNiM7sFeBN4sRr3ex1oEtN1dQgwu1pRBzEMJpgq/O+osubVvY583777BomktBQOPnj3B1rpoVYiEi1uEjGzIuBCd/8fd+/s7me5+3vl5919lbuPB5oAq6t6M3ffBkwHekYVdwNmmNn5ZjazKtcxs7Mi13szquwMggF2SYEOHaCiuW56qJWIlIu72NDdy8zsSjNzgm6sihjBivUp1bjnRGBU5GFXRcB0d19nZm2BjjsvbNYA+CnQCRhqZqvd/QMz24NginFTM7svKo6GwAnViENERGrIKlpaYWabCKbzrqHi7d4N+IG710tPeKnVtWtXLykpqbyiAIn309KKHJHaw8wWunvXeOcSbXvSmmADxr2AT4Fn3P17T6Ews3EpiVJERHJOhUkkMmX2dwBm1h4YFuli+oe7vxpV9fG0RihZ6amn4Lx4ezyLSK1SpQV+7r7E3e9191uBbWZ2g5ldY2Yd3P3LNMcoIanooVZFRcF+W/feq24tkdoumVXin0R+7grgHTMbm9qQJFusWBEkidjjm2+CVsjVV8MVV8C2bWFHKiJhqXISMbMfmdkfgc8JZk09CLRz97vSFZxkpwYN4MknYexYmDQJ+vSB9evDjkpEwpAwiZhZczO72swWA38lWHTYy90Pcvc73H2VmbXMSKSSVQoKgueRTJoEzz8Pp5wCy5eHHZWIZFpFiw0LzOxR4EtgGPAIsI+7X+Duf4+pfkaaY5QsNnw4PPssLF4M3brBhx+GHZGIZFJl60T+BMwjaIHEq1gIXOLuP0pbhCmkdSLps2gR9O4NGzbAn/4Ep50WdkQikirJrhO5jiCJJFIA5EQCkfQ6+mh46y3o1Qt69oSHH4YBA8KOSkTSLW4SiTzG9vnyDRcTMbNZKY9KclK7dvDGG3D++TBwIHz2Gfz614lXvotIbos7JuLu2919cVUu4O5/TW1IksuaNg0G2gcOhJtvDgbgtQuwSP5K6mmCIokUFcH06RWf1y7AIvlDSUTSQl1YIrWDkoiIiCRNSURCUVoadgQikgpKIhKKI4+Ev/0t7ChEpKaURCRtKtoFuEULaN4cfvxjGDMGtmzJbFwikjpKIpI2Fe0CvHo1lJTAiBFw331w3HHw73+HHa2IJENJRELRoAE8+CD85S+wbBl06QJTpuj5JCK5RklEQtW7N7z/frAL8PDhcO65GnQXySVKIhK6Nm2CVe733QcvvhgMus+bF3ZUIlIVSiKSFQoK4KqrYMGCYND9jDOgUaPvb5mibVNEsouSiGSVo44KBt2HD4eNG+PX0bYpItlDSUSyToMGwRMTRST7KYmIiEjSlEQkJ333XdgRiAgoiUiO6tABnnhC60pEwqYkIlmrom1TmjcPZmj17w+nnw6Lq/T4NBFJByURyVoVbZvy9dfBVOBJk4KZXEccAdddBxs2hB2xSO2jJCI5qbAwmAb8n//AxRfD7bfDYYfB00+ri0skk5REJKcVF8OMGfDaa8Hz3fv0CbZS+fTTsCMTqR2URCQvnHwyLFwI994bJJTDD4fGjbXiXSTdlEQkbxQVwejRwUB7nz4Vj5FoxbtI6tTJ9A3NzIChgAP1gNfdfVEFdesDzwC3u/urMed6AB2B7cAmd5+Rvqgll+y1F8yeHUwBFpH0CqMlMgp4292nuvv9wGAzaxpbycz2BO4HDoxzbh/gx+7+oLtPAdaa2UXpDlxERHaX0SRiZoVAP3cviSqeDwyIU/1MYAzwRZxzI4CXo16/AIxOUZhSC4wYAV99FXYUIrkv0y2R7sDmmLLFQP/Yiu4+290rmvnfH/g4qu5WoJmZdUhVoJLfpk2DAw6AsWODx/WKSHIynUTaA2tiytZEyqvEzIqAvat6HTMbYmYlZlZSqkfm1SoVrXhv3Rr+7//gggvgnntg//3hxhth3brMxieSDzKdRIqBspiyMqClmVU1lpYEcce7TnFsZXef5u5d3b1rq1atqhuv5LCKVryvWBG0Qh59FD74AM48E265Bdq3DxYtauW7SNVlOomsAurGlBUBq919RxWvsRrYUcF1VtUsPKltDjsM5syBRYvghBOC7VPat4cJE2Dz5mBNidaaiFQs00lkCRDbHGgGfFbVC7h7GfBlTa8jEq1zZ3juOXjzTejYMVhvcuCBFa8p0VoTkUCmk8jrQJOYrqtDgNnVvM5sgjUiAJhZXeAbd/+o5iFKbXb88fDSS8Gxzz5hRyOS/TKaRNx9GzAd6BlV3A2YYWbnm9nMKl5qEtA76nVPYEJqohSBH/4waJWISGIZX7EOTARGmdl+BOMY0919nZm1ZffWRQPgp0AnYKiZrXb3DwDc/Qsze8zMxgLrgS3u/kiG34fkObPE55csCcZPRGoz81q0b3bXrl29pKSk8ooiEYkSSUEBnHsujBkTDMpXlnREcpWZLXT3rvHOaQNGkQQqWmvSqhWMGwevvAInnQTHHRfs1VUWO/FcJM8piYgkUNFak1Wr4Lbb4IsvYPLkYKFi//7B+pO77tLCRak9lEREaqBRIxg2LFgB/+yzwbTga66BvfeGUaOCFovWmUg+UxIRSYGCAvjJT+Dll4OFi337wpQpFe/LpXUmki+URERSrHPnYEuVpUvDjkQk/ZRERNJkr70Sn//rX2HbtszEIpIuSiIiIendG/bdF66/Hj75JOxoRJKjJCISkqeeCrq+/vd/4aCDoEePoBts48awIxOpOiURkTRK9EyTvn2DTR//+99guvCyZXDZZfCDH8DQobBggXYRluynFesiWcIdXnsNHnkE/vAH2LSp8voimaAV6yI5wAxOOQVmzoTly2Hq1LAjEqmckohIFmraFC6/PHGdOXM0fiLhUxIRyVEXXQTFxcF2K08/HTyJUSTTlEREctTLL8Oll8K8edCnTzBYf9ll8PzzsHVrUEcD85JuSiIiWSzR7K5TTw3GTZYvhxdfhPPOC/bv6tUrSBKDB+vxvpJ+mp0lkke2bIG//Q2efBKeeQbWr6+4bi36X19qKNHsrDCebCgiaVKvXrAR5E9+EkwRbtiw4rpr10KzZpmLTfKTurNE8lSDBonPFxfDaafB/fcHj/oVSYaSiEgtNXZs8NCtq64KHqZ1xBHBPl7z58OOHUEdDcxLZZRERPJYooH5226DDz+Ejz+Ge++Fli3hjjugWzdo2xaGDNHAvFROA+sistOaNcEU4WefDWZ8ffddxXVr0Z+OWk/bnohIlTRvHqw9mTMHSksT133hBa2YFyUREalAvXqJz591VpB0zjgD7rkn6BpT66T2URIRkaS8+CIMHx5sYf/LX0LHjrDPPvDznwe7EK9dq4H52kBjIiJSoTZt4g+it24dzOwq98UXMHducMybB998AwUFu2Z5xVOL/vTkPI2JiEhSVqwI/tjHHtEJBIIWyC9+EbRAVq+Gf/wDbrgh8bUTJRjJHUoiIpJSderACSfATTclrteyZfB0xwcegA8+UMskV2nbExEJRZ8+wU7Ef/5z8Lq4OHjO/KmnBsfBBwePCq5Kd5qER0lEREIxfXrwdelSeOWVIKG88kowvRhgr7202DEXqDtLRNIm0Yr5cvvtBwMHwu9/HwzQ/+c/8NBDcPLJia+9fXvKwpQa0OwsEclaZhWfa9IEjj8eTjopOI499vu7Fld1dpkkpq3gRSTvXHwxvPEGjB8fvK5TB7p02ZVUTjxR3WGZoJaIiGStRC2R8j9da9bAP/8ZJJQ33oAFC3Y9HjiRWvSnr8ayqiViZgYMBRyoB7zu7ovi1KsHDAc2AnsAT7n7Z1HnTwJOAdYChwJ3u/vn6X8HIpIprVtX3B1Vrnnz4JHAvXoFrzdvhoULg4Ry7bUVX/v554MusJYtUxtzbZPxloiZXQW84e4lkddTgGvd/ZuYencCE9z9q0jimQkMdPftZtYKmOzuF0Tqtgemu/upie6tlohI7ZKoJVPugAOC7e+POy44OnWCunWDcxpTCWRNS8TMCoF+7j4hqng+MAC4P6peM6Czu38F4O5uZkuA3sAzwMnAzlaJuy8xs33T/w5EJF+88krwAK7584PpxY8/HpTXqwedOwcJRWMqlct0d1Z3YHNM2WLgPqKSCHAusCxOvf4ESeQroLuZ1XH3bZGWyeL0hCwiuSpRd1iPHsEBwfjIl1/uSirz58O0aYmv7V61lk6+y3QSaQ+siSlbEymvcj13f8vMPgVeNrPrgcOAn8a7oZkNAYYAtGvXrkbBi0huqWqXk1mw/9c++8D55wdlZWW7urXiad06mA3WpQscfXTwtV273RNLbegOy3QSKQbKYsrKgJZmVuDuO6LqrYtTrzjq9QjgTmA0cDCwCFgde0N3nwZMg2BMpKZvQERqh6KixOd79w4G8OfN27XwsWXLXQmlS5fa0R2W6SSyCojN7UXA6qgEUl6vcZx6qwDMrAXwJHC2u280s17AXDM73N2Xpyd0EZFdHnkk+LppE7z3XpBQyo+77oJt28KNL1MynUSWAK1iypoRNUgeVe+0BPX6Av92940A7v5XM3sROB94IKURi0itVZUpxg0a7JrZVW7zZnj//WAKcUW6dAlmgh111K5jzz13r5ML3WGZTiKvA01iuq4OAWbH1HsaGBxTFl2vMd8foF8Wp0xEJGnJ/qGuXx+OOSZxnebN4S9/2dWiAdh3310JpVOn3OgOy2gSicykmg70BJ6PFHcDfmVm5wM/cffL3H2tmc2PdE99GKl3IPCbyPd/B6abmUWm/xYCXYC7M/h2RESSNm/ergd8vfsuvPNO8PXdd+G553LnoV1hLDY0YBSwlWCc4x/uXmJmo4CfuXuXSL16wBiCFZfwWZ0AAAbNSURBVOmNgT+5+5Ko6/QFTgX+A7QF5sRb+R5Niw1FJJOS7Y7auBE+/DBxd1ibNsFz7aOPww+Hxo13r5OK7rBEiw21d5aISJZKtA5lwIDgiZAffhgM7pfbf/9dSeX22yv++er86c+aFesiIpIav/td8HXHDvjssyChRB8vvJCZOJRERESyVFVmhxUUBPt/HXAAnHPOrvKtW4MtXNJNSUREJEvVZBpvotX2qaTH44qISNKURERE8lRVnnFfU+rOEhHJU5lY1a6WiIiIJE1JREREkqYkIiIiSVMSERGRpCmJiIhI0mrV3llmVgp8nuSPtyTOkxOlyvT51Zw+w5rR55e8fd099llQQC1LIjVhZiUVbUAmldPnV3P6DGtGn196qDtLRESSpiQiIiJJUxKpumlhB5Dj9PnVnD7DmtHnlwYaExERkaSpJSIiIklTEhERkaQpiVTCAsPMbKiZjTKzo8OOKReZ2SYz86ijbdgxZSszKzCz35nZgJjyI83sqsjv4ggz0/+/cST4/MbF/A4+HFKIeUW/hJUbBbzt7lPd/X5gsJk1DTuoXGJmdYCxQIPyw92XhRtVdjKzesA9QOeY8sbAcHef4O5TgX8QfKYSpaLPL+I7oBG7fg+HZTC0vKUkkoCZFQL93L0kqng+MCCciHJWE2C5u28uP8IOKIv1BG4D3okpvxRYWP7C3d8B+ppZhh6CmjMq+vwAytx9Y9Tv4bYMx5aXlEQS6w7E/sFbDPQPIZZctgewNuwgcoG7P+PupXFO9Qc+jilbB/ww/VHljgSfH8D2jAZTSyiJJNYeWBNTtiZSLlXXBJhoZhvM7D0zOyvsgHKQfhdrrquZfWlmq81supk1CTugfKAkklgxUBZTVga01KBmtWwC/hc4CpgMPGFmXcINKedU9LtYHEIsuWoRcGbk2BeYGW44+UHPWE9sFRDb51wErHb3HSHEk5Pc/RPgk8jLTyKDxEOAy8OLKudU9Lu4KoRYcpK7/7b8ezM7l+B38QfuvjzEsHKe/jWd2BIgdvvjZsBnIcSST/5G8C9BqTr9LqaQu68H3kS/hzWmJJLY60CTmK6rQ4DZIcWTL5oDn4YdRI6ZDXSMKWsGvBRCLPliT5SEa0xJJIHIFMDpBNMGy3UDZoQSUI4ys0vMrFHk+zrACGBCuFHlnMeAk83MIFh4CDzt7lvDDSs3mNkRZnZC1OsewL/dfWV4UeUHbcBYicj/tKOArQR90P+IWTciCUQ+v+lAV+DPBJ/jHHePna4q7Py8fgrcQjCl9wZ3fyty7gjgbOBrgvHMKe6uaatRKvr8zOwU4EHg38ACgunRM/T51ZySiIiIJE3dWSIikjQlERERSZqSiIiIJE1JREREkqYkIiIiSVMSERGRpCmJiIhI0pRERJIQWQF9TeQxq0+Z2aVRx+Vm9m1kdX667l9oZleYWZmZ7Zeu+4hURosNRWrAzBwY6O4zYsrvdfcxGbj/58Ap7r403fcSiUctEZH0eDBD99G/AiVUSiIiKWZmh7n7ksj3DcxsYtgxiaSLkohI6p0JYGYtgbeAo8xsiJm9YGYrI19/UF7ZzA40s9vM7GIzG2Nm95pZ/ZjzvzOzu8zsczN7onw334g9zWySmX0SuXa9jL1TqfU0JiJSA5ExkUeBf0aKioAx7r5/5PyZBI8EPsfd34s81XEusMbdf2JmzYH5wLHuvjbyM4MjrwdHEsL7wOXu/oqZ7UPwRMjx7u5mthT4E3A9sA14GZjq7o9n5AOQWk+PxxWpuVeiB9bNbM+oc5uAz939PQieqGdmNwB/N7O6wEBgUXkCiZgJTDKz64EOQD13fyXy818AN8Tcf7K7b4rc+1WgfSrfnEgi6s4SSb0/VXL+fYL/9/Yg+IP/VfTJyIOm1gAHAW2Abyq53rao77cDhdUJVqQmlEREUszdP4SdXVmN41RpDKxw968Jnp3eMvpkpIXSAlgaOd8+0g0mknWURETSwMwKCcYu1hOTJIBfADdFvp8JHGtmTaLODwBmufuyyFM0PwAmRK6JmV0UGbQXCZ3GRESSYGZdgY6RlxfEtBTqAD8GmkdeNzGzqwm6qNoBX7v7VAB3X21mfYDrzexfQCOCLqwhUdfrB8wAvjSzJcA9wDdm9nOCFsu1ZnZv1H23mdlf3H1hqt+3SCzNzhJJIzPrAfza3Xuk4dqF7r49Mt3X3H1Hqu8hUhm1RERylLtvj3x1tHJdQqIxERERSZqSiEiaRBYS9gY6mNlPzaw47JhEUk1jIiJpEtkKvgjYTLB2w9y9LNyoRFJLSURERJKm7iwREUmakoiIiCRNSURERJKmJCIiIkn7/+V5MRkfciuyAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "train(epochs=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "273.188px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
